Pass Full Array between view controllers in swift

夙愿已清 提交于 2019-12-14 02:34:04

问题


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



来源:https://stackoverflow.com/questions/26998170/pass-full-array-between-view-controllers-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!