Lavvo
Lavvo

Reputation: 1024

How to Pass Array Object between view controllers by reference

I am trying to pass an Array Object between 2 view controllers. But I want the Array to be passed as a Reference and not copied. I know if I was using a function locally, I can use "inout" and "&" based on the examples I found.

But I what I want to do is slightly different. I want to assign the Array in VC1 directly to the Array object in VC2 as a reference. Here is my code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "vc2" {

        let vc2 = segue.destinationViewController as! ProfileVC

        vc2.data = &self.data       //I know this line doesn't work, how would I go about it?

    }

}

Upvotes: 0

Views: 147

Answers (1)

Vincent Bernier
Vincent Bernier

Reputation: 8664

Swift array are struct, so they will get copied.

The simplest way would be to use an object instead of a struct. So I would use a NSMutableArray instead.

With that you will have all the power of mutability at your finger tips ;-)

Upvotes: 1

Related Questions