ios - swift 错误 : Value of type 'NSObject -> () ' AnimalListTableViewController' has no member 'tableView'

标签 ios xcode swift

我正试图在 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]
    }
}

最佳答案

代码格式不正确。

在生成错误的行中,您在 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/

相关文章:

ios - 类型 'string' 的值没有成员 'containsString'

ios - @objc 之后调用的所有代码都需要符合 Objective-C 吗?

ios - collectionview 只填充 iPhone 4-5 上的一个单元格

swift - 如何在swift中检查变量的类型

ios - 使用 Parse Swift 检索行值和列名

ios - 如何在iOS 10之前的版本中使用UNNotificationServiceExtension运行应用程序?

ios - 谷歌登录 IOS Swift 4 AppDelegate 错误

json - 无法使用SwiftyJSON访问JSON数据

c - Xcode 4.3中getline c函数的实现在哪里?

swift - 键盘隐藏/显示通知时 UITextView 的 ScrollView 问题