斯威夫特错误:类型的值“NSObject的 - >()‘AnimalListTableViewController’没有成员‘的tableView’
问题描述:
我试图在迅速的Xcode斯威夫特错误:类型的值“NSObject的 - >()‘AnimalListTableViewController’没有成员‘的tableView’
摆脱这些错误的。如果截图是太小,这里是代码
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
类的常数数据成员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
语句折叠为单个语句。清洁器;避免可选项。