Pass Full Array between view controllers in swift

2019-08-05 06:23发布

问题:

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:

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