我是一个代码初学者,实际上我几周前就加入了这个世界。我正在尝试使用Xcode和Swift为Ios构建我的第一个应用程序。我想创建不同的表视图并在这些表之间传递数据。
现在,我编写了代码,但一直收到“预期声明”错误。我真的不知道怎么解决这个问题。有人能帮帮我吗?谢谢。
//
// 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**发布于 2016-07-18 16:08:44
这里的错误是您的cellForRowAtIndexPath函数中的prepareForSegue函数。
它们应该在全局范围内
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
}发布于 2016-07-18 16:20:18
从你得到的错误开始,“预期声明”错误是因为你的类中缺少大括号"}“。
解决此错误将给您另一个警告,因为在return语句之后编写的代码永远不会执行。您已经在返回语句之后编写了prepareForSegue函数。
现在主要的问题是,prepareForSegue必须在全球范围内,就像@Özgür Ersil在他的回答中提到的那样。因此,为您的类添加一个右大括号。从cellForRowAtIndexPath函数中删除prepareForSegue函数,并在类内的全局范围内编写它。
希望本文能对您有所帮助:)
https://stackoverflow.com/questions/38431522
复制相似问题