Swift错误:类型为'NSObject -> ()'的值'AnimalListTableViewController'没有成员'tableView'。

3

我正在尝试解决Swift Xcode中的这些错误。

Value of type 'NSObject -> () -> AnimalListTableViewController' has no member 'tableView' / Consecutive declarations on a line must be separated by ';' / Variable used within its own initial value

如果截图太小,这里是代码。
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]
    }
}
1个回答

1
代码格式错误。
在产生错误的行中,您正在 AnimalListTableViewController 类声明的上下文中工作,而不是在函数内部。从左到右阅读时,就好像您正在尝试声明 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 语句成为单个语句。更简洁;避免可选项。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接