我正试图在 swift xcode 中消除这些错误
如果截图太小,这里是代码
import UIKit
class AnimalListTableViewController: UITableViewController
{
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
let indexPath = self.tableView.indexPathForSelectedRow()//this is where the error appears, it says Value of type 'NSObject -> () -> AnimalListTableViewController' has no member 'tableView'
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?)
{
if let DetailViewController =
segue.destinationViewController
as? DetailViewController {
}
}
if let indexPath = self.tableView.indexPathForSelectedRow()
{
DetailViewController.Animal = animals[indexPath.row]
}
}
最佳答案
代码格式不正确。
在生成错误的行中,您在 AnimalListTableViewController
class
声明的上下文中工作,而不是在函数中。从左到右阅读时,就好像您正在尝试声明 AnimalListTableViewController
类的常量数据成员 indexPath
。
看起来你正在尝试这样做:
class AnimalListTableViewController: UITableViewController
{
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?)
{
if let detailViewController = segue.destinationViewController as? DetailViewController, let indexPath = self.tableView.indexPathForSelectedRow {
detailViewController.Animal = animals[indexPath.row]
}
}
}
还清理了一些其他东西:
- 不要同时使用类名 (
DetailViewController
) 作为变量名。将其更改为detailViewController
。 - 将
if let
语句折叠成一条语句。清洁工;避免可选。
关于ios - swift 错误 : Value of type 'NSObject -> () ' AnimalListTableViewController' has no member 'tableView' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35661838/