2

When using Objective-C I would pass a NSMutableArray from one view controller VC_A to another VC_B by simply assigning a property in VC_B as

VC_B.list = self.list

where self is VC_A

It allows the changes done in VC_B on the list to be seen in the list in VC_A when the view controller was say popped off the navigation stack.

However in Swift as arrays are passed by value, assigning as above does not work so I am stuck how to solve this. What would be the correct way to handle this now?

4
  • 1
    repass the array to VC_A when you are popping off your VC_B Commented Aug 17, 2015 at 11:32
  • 1
    stackoverflow.com/questions/25533122/… Commented Aug 17, 2015 at 11:42
  • I think this blog post explains that Swift is focused on representing basic data structures as values, not references for convenience - medium.com/swift-programming/… I suspect that you could use NSArray to get something similar to the objective-c style, but Swift has different way of doing things and you might want to reevaluate your approach to be more Swift-ish. Commented Aug 17, 2015 at 11:46
  • 1
    I'd argue that even in Objective-C, this is the wrong solution to the problem. Giving classes mutable public properties for the sake of relying on this sort of thing is a pretty dangerous proposition... especially once you get into the realm of multithreading your app at all. Commented Aug 17, 2015 at 12:11

4 Answers 4

3

You can still do this in Swift by making the property an NSMutableArray, just as before. Foundation types still exist if you ask for them. But this is bad design in both ObjC and Swift. It creates spooky action at a distance (when things magically change values that were not part of the call) and likely breaks MVC (if the data is persistent, it should live in the model, not in the view controllers).

There are two common patterns in Cocoa: store the data in the model, or pass it via delegation.

If this array represents some kind of persistent state (such as a list of items in the system), then that belongs in the model layer, and both view controllers should read and manipulate it there rather than by communicating with each other. (See Model-View-Controller.)

If this array is a transient piece of data (such as selections from a list), then the calling VC should set itself as the delegate to the receiving VC, and when the receiving VC finishes, it should pass the data back to its delegate. (See Delegates and Data Sources.)

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

1 Comment

Using an NSMutableArray allowed me a drop in solution for time being
2

If you use the standard Swift Array which is a value type you have to use a wrapper or a untyped NSArray.

// a generic wrapper class
class Reference<T> {
    var value: T
    init(_ val: T) { value = val }
}

// Usage
class ViewController1 {
    static var list = Reference<[Int]>([])
}

class ViewController2 {
    static var list = Reference([3, 5, 7, 9, 11])

    func passToOtherVC() {
        ViewController1.list = self.list
    }
}

If you want to mutate the array you should always change the value property of the Reference object.

Comments

1

In Swift, objects are automatically passed by reference. NSArray is an Objective C class (pass by reference), where as Array is a struct (pass by value).

So if you are working with NSMutableArray the array is already being passed by reference.

Comments

-1

Just as a potential proof of concept that complements my comment on the question - it is possible to use the Objective-C NSMutableArray to accomplish this task:

class A {
    var x: NSMutableArray = NSMutableArray(capacity: 12)
}

class B {
    var y: NSMutableArray!
}

let a = A()
let b = B()

b.y = a.x

b.y[0] = 123

assert(a.x[0] === b.y[0])

Still, this is approach is not following the Swift style of handling data structures IMO.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.