0

How do I keep the reference to an array after an items is appended?

Updated code example, because the prior example didn't seem to be clear enough.

class ViewController: UIViewController {

    var numbers = Numbers.singleton.numbers

    override func viewDidLoad() {

        print(numbers.count)

        Numbers.singleton.add(1)

        print(numbers.count) // prints 0
        print(Numbers.singleton.numbers.count) // prints 1
    }
}

class Numbers {

    static let singleton = Numbers()

    var numbers: [Int]!

    private init() {
        numbers = []
    }

    func add(number: Int) {
        numbers.append(number)
    }
}
Manuel
  • 14,274
  • 6
  • 57
  • 130
  • 1
    I think you switched the outputs - the first print prints 1, the second one 0. – luk2302 May 20 '16 at 14:09
  • @luk2302 Whoops, I did! – Manuel May 20 '16 at 14:10
  • What's the problem? You changed `self.array` and it worked. Your `arrayRef` is a pointless copy; just delete it from the story. – matt May 20 '16 at 14:12
  • What is the reason of creating a reference to an array? And, can you change the type of array (to NSArray)? – kennytm May 20 '16 at 14:12
  • @matt But how can I reference to the array? I want to reference to the array from outside the class. The above is just an example to demonstrate it. – Manuel May 20 '16 at 14:13
  • 1
    https://developer.apple.com/swift/blog/?id=10 - *"In Swift, `Array`, `String`, and `Dictionary` are all value types."* – luk2302 May 20 '16 at 14:14
  • But that isn't at all what you asked. To refer to the array from outside the class, get a reference to this view controller and talk of `theViewController.array`. It is a public property. – matt May 20 '16 at 14:15
  • @matt Thanks, the construct is more complicated for the simple question. I just wanted to understand why I cannot reference to it. Now it makes sense since it is a value type. – Manuel May 20 '16 at 14:18

1 Answers1

2

Arrays in Swift don't have "references". They are structs, and a struct is a value type. Your (badly named) arrayRef is a separate copy, not a reference to self.array.

Moreover, there is no good reason to want to do what you (seem to) want to do. To have two simultaneous references to a mutable array would be unsafe, since the array can be changed behind your back. The Swift design is sensible; use it, don't subvert it.

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • So if I use NSArray instead I could reference to it, since it is a class? – Manuel May 20 '16 at 14:16
  • 1
    Certainly. But you still couldn't _change_ the array (append to it) through NSArray, as NSArray is immutable. That is how Cocoa provides the same kind of safety that Swift does. – matt May 20 '16 at 14:17
  • So I updated the code example to closer resemble the actual code. The `numbers` array can indeed be changed by other processes in the app. The reference to the array is defined as a property with `var numbers = Numbers.singleton.numbers` to be available throughout the class. But that is actually copying the array as I understand. So what would be a good design for this - only access it through with `Numbers.singleton.numbers` throughout the class? – Manuel May 20 '16 at 14:27
  • I still don't know what you are trying to do (or what you think you are trying to do). What are your goals for being able to access / change this array? – matt May 20 '16 at 14:44
  • The `numbers` array contains data points that are collected over time. To make the data points available throughout the app they are stored in a singleton class. The adding of data points happens by another process in the app which also triggers a NSNotification for all observers of the data points. When a new data point is added the control to display the data points should reload it's data source, i.e. the `numbers` array. So to handle the notification I should update the data source by copying the array from the singleton class as I understood from your explanation. – Manuel May 20 '16 at 14:50
  • You don't need to "copy" anything. Your array is a publicly visible `var` property; anyone can `append` to it. Again I ask, what's the problem? – matt May 20 '16 at 14:56
  • Thanks matt. My original question is already answered because I didn't know that an array was a value type and that caused the behaviour I didn't understand. – Manuel May 20 '16 at 15:38