Swift: can I set an object property to an array reference?

I have a `UITableViewController` that can display content from any of three different arrays. In Objective-C, what I would typically do is set a property on the `TableVC` that is a reference to the array to be displayed. Then the `TableVC` would not only be able to show the contents of that array, but also handle user-directed editing of the array, say, if they deleted or re-ordered elements.


The way it seems to work in Swift when I set a property on my `TableVC` to an array is that it is a _copy_ of my model's array, not a reference. And while this is fine in certain contexts, in my application it seems bad for two reasons: 1) my arrays are enormous, thousands of elements, and copying huge arrays over and over again seems wasteful; 2) small edits are tougher to handle: I have to communicate back to the model about element deletion or re-ordering and make sure my view and model arrays stay in sync.


Setting a property that's a reference to an array is what I _want_ to do, but there's no such thing as an `inout` object property in Swift.


What is the correct way for me to handle this?


Thanks.

Accepted Reply

There are four (well, at least four) ways to work around this:


1. If the arrays are themselves properties of other objects, you can store KeyPath values representing the properties. This requires that you have known references to the objects with those properties. (That's not an uncommon situation, though it obviously doesn't apply everywhere.)


2. You can wrap the array properties in a reference type, and store the reference. Note that your use case implies that the array contents are the "one true truth" of something, since you want to edit the original values. That in turn suggests that the array values should be — or should be in — a reference type.


3. Make each of the types that can provide arrays conform to a protocol. You are, in fact, wanting that for your use case — you require each source of content to provide an array that can be edited.


4. Use NSArray instead of Array.

Replies

There's no reference var to value type in Swift. You may need to define your own reference type containing the array.

Or else you can use `NSArray`, but that seems un-Swifty.


If you want to discuss more specific things to your TableVC, you should better show your code, at least telling the outlines how your TableVC manages the reference to the array.

There are four (well, at least four) ways to work around this:


1. If the arrays are themselves properties of other objects, you can store KeyPath values representing the properties. This requires that you have known references to the objects with those properties. (That's not an uncommon situation, though it obviously doesn't apply everywhere.)


2. You can wrap the array properties in a reference type, and store the reference. Note that your use case implies that the array contents are the "one true truth" of something, since you want to edit the original values. That in turn suggests that the array values should be — or should be in — a reference type.


3. Make each of the types that can provide arrays conform to a protocol. You are, in fact, wanting that for your use case — you require each source of content to provide an array that can be edited.


4. Use NSArray instead of Array.

Hi can you give a simple example please? in understand the concept but I really need to how it works. many thanks in advance k