0

I'm trying to learn swift. When I do:

var myIntArray = [Int](count: 3, repeatedValue: 0)
myIntArray[0] = 1
myIntArray[1] = 11
myIntArray[2] = 111

// prints "my array: [1, 11, 111]"
NSLog("my int array: [%d, %d, %d]", myIntArray[0], myIntArray[1], myIntArray[2])

It works as expected, but when I wrap the int in a class like this:

class Foo {
    var bar = 0
}

var myWrappedIntArray = [Foo](count: 3, repeatedValue: Foo())
myWrappedIntArray[0].bar = 1
myWrappedIntArray[1].bar = 11
myWrappedIntArray[2].bar = 111

// prints "my array: [111, 111, 111]"
NSLog("my wrapped int array: [%d, %d, %d]", myWrappedIntArray[0].bar, myWrappedIntArray[1].bar, myWrappedIntArray[2].bar)

It seems to modify the entire array when trying to modify a single element. What am I doing wrong here?

5
  • Perhaps my argument for repeatedValue creates a single Foo instance and fills the array with pointers to this instance? But how to fix that? Commented Sep 12, 2016 at 10:27
  • Why aren't you using 'print' instead of "NSlog" ? Commented Sep 12, 2016 at 10:29
  • @RashmiRanjanmallick Because I'm used to using NSLog in iOS development, is that really relevant to the issue at hand? Commented Sep 12, 2016 at 10:30
  • No not relevant. I was just curious about that. Commented Sep 12, 2016 at 10:35
  • @MartinR thanks, that's what I was looking for Commented Sep 12, 2016 at 10:46

1 Answer 1

1

For this case it is better to use Struct:

struct Foo {
    var bar = 0
}

var myWrappedIntArray = [Foo](count: 3, repeatedValue: Foo())
myWrappedIntArray[0].bar = 1
myWrappedIntArray[1].bar = 11
myWrappedIntArray[2].bar = 111

// prints "my array: [1, 11, 111]"
print("my wrapped int array: [%d, %d, %d]", myWrappedIntArray[0].bar, myWrappedIntArray[1].bar, myWrappedIntArray[2].bar)

In you're case you are working with reference type value such as [Foo]. But Struct is value typed and create each element of the array unique.

You are referencing to the same object in myWrappedIntArray[0].bar and myWrappedIntArray[n].bar.

Because of the constructor.

var myWrappedIntArray = [Foo](count: 3, repeatedValue: Foo())

You repeat Foo() reference for each element of the array instead of the creation of the new class for each index of the array.

Sign up to request clarification or add additional context in comments.

4 Comments

My use case is different, but this is the smallest example I could write that reproduces my issue
i edit my answer.
How can I create the array with a Foo instance for every element? Do I need to create the array empty and append the Foo instances manually or is there an equivalent of init(count: repeatedValue:) that creates a new instance for every element?
One of the ways you describe. Take a look at answer of the MartinR. stackoverflow.com/questions/32921425/… For you if it is possible use Structs instead of classes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.