在表视图之间使用Structs和Arrays传递数据(Swift:Xcode)

我是一个代码初学者,实际上我几个星期前join了这个世界。 我试图通过使用Xcode和Swift来为Ios创build我的第一个应用程序。 我想创build不同的表视图并在这些表之间传递数据。

现在,我编写了代码,但是我一直得到“预期声明”错误。 我真的不明白如何解决这个问题。 有人可以帮帮我吗? 谢谢。

// // ViewController.swift // Tot_Forum // // Created by Fausto Saltetti on 18/07/16. // Copyright (c) 2016 Fausto Saltetti. All rights reserved. // import UIKit class FirtTableViewController: UITableViewController { var FirstTableArray = [String]() var SecondArray = [SecondTable]() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. FirstTableArray = ["Focus on learning", "Participate and network", "Access and build knowledge", "Assess, reflect, evaluate", "Inspire and generate ideas", "Problem solve and plan", "Map ideas and relationships"] SecondArray = [SecondTable(SecondTitle: ["After Action Review","Audience Response Systems","Blogs","Case Studies", "Discussion Forums UPDATE","Jigsaw","Peer Assist", "Podcasting","Presentations", "Role Play", "Screencasting", "Social Networking", "Sociometrics"]), SecondTable(SecondTitle: ["After Action Review","Audience Response Systems","Blogs","Case Studies", "Discussion Forums UPDATE","Jigsaw","Peer Assist", "Podcasting","Presentations", "Role Play", "Screencasting", "Social Networking", "Sociometrics"]), SecondTable(SecondTitle: ["After Action Review","Audience Response Systems","Blogs","Case Studies", "Discussion Forums UPDATE","Jigsaw","Peer Assist", "Podcasting","Presentations", "Role Play", "Screencasting", "Social Networking", "Sociometrics"])] } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return FirstTableArray.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell Cell.textLabel?.text = FirstTableArray[indexPath.row] return Cell func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()! var DestViewController = segue.destinationViewController as! SecondTableViewController var SecondTableArrayTwo : SecondTable SecondTableArrayTwo = SecondArray[indexPath.row] DestViewController.SecondArray = SecondTableArrayTwo.SecondTitle } } **//Error here: !Expected declaration** 

这里的错误是你的prepareForSegue函数在一个cellForRowAtIndexPath函数中。

他们应该在全球范围内

var SecondTableArrayTwo:SecondTable? 应该用init来解包或者可选。

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var Cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell Cell.textLabel?.text = FirstTableArray[indexPath.row] return Cell } func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()! var DestViewController = segue.destinationViewController as! SecondTableViewController var SecondTableArrayTwo : SecondTable? SecondTableArrayTwo = SecondArray[indexPath.row] DestViewController.SecondArray = SecondTableArrayTwo!.SecondTitle } 

从你得到的错误开始,“预期声明”错误是针对你class上缺失的大括号“}”。

解决这个错误会给你另一个警告,因为在return语句之后编写的代码永远不会被执行。 您在return语句之后编写了prepareForSegue函数。

现在,主要的问题,准备论争,必须在全球范围内,正如@ÖzgürErsil在他的回答中所提到的那样。 因此,为你的class级添加一个右大括号。 从cellForRowAtIndexPath函数中删除prepareForSegue函数,并将其写入全局范围内的类中。

希望对你有帮助 :)