1
votes

I am trying to pass a full array between view controllers but cannot figure out the missing piece.

In view controller one I have:

protocol ExclusionsViewViewControllerDelegate{
    func ExcUpperDidFinish(controller:ExclusionsView)
    func ExcLowerDidFinish(controller:ExclusionsView)

}

class ExclusionsView: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var delegate:ExclusionsViewViewControllerDelegate? = nil
    var ExcLowerArray:[Int]=[]
    var ExcUpperArray:[Int]=[]

    @IBOutlet var ExcLowerText: UITextField!
    @IBOutlet var ExcUpperText: UITextField!
    @IBOutlet var ExcFreqTable: UITableView!

    @IBAction func BackButton(sender: AnyObject) {
         if (delegate != nil){
            delegate!.ExcUpperDidFinish(self, Array: ExcUpperArray)
            delegate!.ExcLowerDidFinish(self, Array: ExcLowerArray)
        }
        dismissViewControllerAnimated(true,completion:nil)
    }

In View controller two I have:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,          PreferencesViewControllerDelegate, ExclusionsViewViewControllerDelegate {

var ExcUpperFreqArray:[Int]=[]
var ExcLowerFreqArray:[Int]=[]

override func viewDidLoad() {
    super.viewDidLoad()
}

func ExcLowerDidFinish(controller: ExclusionsView, Array:[Int]) {
    ExcLowerFreqArray = Array
    controller.navigationController?.popViewControllerAnimated(true)
}
func ExcUpperDidFinish(controller: ExclusionsView, Array:[Int]) {
    ExcUpperFreqArray = Array
    controller.navigationController?.popViewControllerAnimated(true)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "pushExclusions"{
        let zc = segue.destinationViewController as ExclusionsView
        zc.ExcLowerArray = ExcLowerFreqArray
        zc.ExcUpperArray = ExcUpperFreqArray
    }
}

I cannot figure out how to correctly reference the array. I am trying to create the array ExcLowerArray in view controller one, and when I change view it will copy all data to the array ExcLowerFreqArray in the second view controller, so that I can reference it in that view controller. At the moment though I get an error on these two lines: delegate!.ExcLowerDidFinish(self, Array: ExcLowerArray) func ExcLowerDidFinish(controller: ExclusionsView, Array:[Int]) {

1
Could you elaborate a little bit on what the problem is? You want array references to be passed, but copies are created instead?Antonio

1 Answers

2
votes

Swift arrays are value types, and as such they are passed not by reference but by value, which means a copy is created when an array is passed to a function, assigned to a variable, etc.

In order to pass an array (and more generally any value type) to a function by reference, you can use the inout modifier in the function declaration, and use the reference operator & when passing the argument to the function. In your case:

func ExcLowerDidFinish(controller: String, inout Array:[Int])

and:

delegate!.ExcLowerDidFinish(self, Array: &ExcUpperArray)

Off topic: note that by convention in swift functions/methods and variables/parameters names start with lowercase, whereas types (classes, structs, etc.) start with uppercase. Your code may be hard to read by other Swift developers