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.
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.