1

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? Commented Nov 18, 2014 at 15:46

1 Answer 1

2

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

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

2 Comments

Thank you for your answer, and thank you for the convention note, as you can tell I am new to coding.This has fixed any errors within my code. If I create some values in view controller two and go to view controller one all values are copied to my array. However if I add values in view controller one and then move to two, none of the values are copied to the other Array. So my segue is working with regards to the two arrays. But the function isn't properly working. Can you see any issues with this code, or would I be best just creating the prepareForSegue method in both view controllers?
I see that in ExcLowerDidFinish and ExcUpperDidFinish the array passed in is copied to a local property. That might be the problem. In that case, you might create a class containing the 2 arrays, replace the 2 array properties with an instance of such class, and pass it to functions - being classes reference types, they are passed by reference. Maybe not an elegant way to solve the problem.

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.