快速在视图控制器之间传递完整数组 [英] Pass Full Array between view controllers in swift
问题描述
我试图在视图控制器之间传递完整的数组,但无法找出丢失的部分。
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)
}
在View Controller中有两个:
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
}
}
我不知道如何正确引用数组。我试图在视图控制器1中创建数组ExcLowerArray,当我更改视图时,它将所有数据复制到第二个视图控制器中的数组ExcLowerFreqArray,以便可以在该视图控制器中引用它。目前,尽管我在这两行中遇到错误:
代理!.ExcLowerDidFinish(自己,数组:ExcLowerArray)
函数ExcLowerDidFinish(控制器:ExclusionsView,数组:[Int]){
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]) {
推荐答案
快速数组是值类型,因此它们不是通过引用而是通过值传递,这意味着在数组为
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.
为了通过引用将数组(通常是任何值类型)传递给函数,可以使用函数声明中的 inout
修饰符,并将参数传递给函数时使用引用运算符&
。在您的情况下:
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])
,并且:
delegate!.ExcLowerDidFinish(self, Array: &ExcUpperArray)
关闭主题:请注意,按常规使用swift函数/方法和变量/参数名称以小写字母开头,而类型(类,结构等)以大写字母开头。您的代码可能很难被其他Swift开发人员阅读
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
这篇关于快速在视图控制器之间传递完整数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!