【问题标题】:swift: Cannot override with a stored property 'tableView' [duplicate]swift:不能用存储的属性'tableView'覆盖[重复]
【发布时间】:2017-03-24 07:48:31
【问题描述】:

我有以下代码:

import UIKit

class SearchTableViewController: UITableViewController, UISearchBarDelegate{

    final let urlString = "http://tvshowapi.azurewebsites.net/tvshownewsfeed"

    @IBOutlet var tableView: UITableView!

    var nameArray = [String]()
    var favouriteCountArray = [String]()
    var imgURLArray = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        createSearchBar()

        self.downloadJsonWithURL()

    }

    func createSearchBar() {
        let searchBar = UISearchBar()
        searchBar.showsCancelButton = false
        searchBar.placeholder = "Search a show"
        searchBar.delegate = self
        searchBar.backgroundColor = UIColor.black
        searchBar.barTintColor = UIColor.black
        self.navigationItem.titleView = searchBar
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.black

        let searchTextField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField

        searchTextField?.textAlignment = NSTextAlignment.left
    }

    func downloadJsonWithURL() {
        let url = NSURL(string: urlString)
        URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in
            if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [[String:Any]] {

                for tvshow in jsonObj! {
                    if let name = tvshow["screenName"] as? String {
                        self.nameArray.append(name)
                    }
                    if let cnt = tvshow["favouriteCount"] as? Int {
                        self.favouriteCountArray.append("\(cnt)")
                    }
                    if let image = tvshow["imageUrl"] as? String {
                        self.imgURLArray.append(image)
                    }
                }


                OperationQueue.main.addOperation({
                    self.tableView.reloadData()
                })
            }
        }).resume()
    }

    func downloadJsonWithTask() {

        let url = NSURL(string: urlString)

        var downloadTask = URLRequest(url: (url as? URL)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20)

        downloadTask.httpMethod = "GET"

        URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in

            let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)

            print(jsonData!)

        }).resume()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return nameArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SearchTableViewCell
        cell.nameLabel.text = nameArray[indexPath.row]
        cell.dobLabel.text = favouriteCountArray[indexPath.row]

        let imgURL = NSURL(string: imgURLArray[indexPath.row])

        if imgURL != nil {
            let data = NSData(contentsOf: (imgURL as? URL)!)
            cell.imgView.image = UIImage(data: data as! Data)
        }

        return cell
    }


}

Xcode 出现三个错误:

  1. 不能用存储的属性“tableView”覆盖
  2. 带有 Objective-C 选择器“tableView”的“tableView”的 Getter 与具有相同 Objective-C 选择器的超类“UITableViewController”中的“tableView”的 getter 冲突
  3. 带有 Objective-C 选择器“setTableView:”的“tableView”设置器与具有相同 Objective-C 选择器的超类“UITableViewController”中的“tableView”设置器冲突

SearchTableViewController 以下是 TableViewCell 的代码

class SearchTableViewCell: UITableViewCell {

    @IBOutlet weak var imgView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var dobLabel: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

我做错了什么?

【问题讨论】:

  • A UITableViewController 已经有一个 tableView 属性,并且它是“自动”连接的。不需要定义自己的tableView 属性。比较stackoverflow.com/questions/34565570/…
  • 只需用@IBOutlet var ttvshowTbl 重命名tableView:UITableView!并从 UITableViewCell 中删除“弱”。现在清理并运行
  • tableView 是apple api 的内置属性,它实际上和你的name 和Apple 属性冲突,你只需要更改属性的名称,清理你的项目并再次运行该项目。

标签: json swift xcode


【解决方案1】:

UITableViewController 类有一个隐含的tableView 属性,其中包含连接的数据源和委托。删除IBOutlet 并使用它。

【讨论】:

    【解决方案2】:

    Xcode 基本上是在告诉你“我已经在使用那个名字了”。将名称更改为其他任何名称,例如 tableView2 或其他名称,它会起作用,因为 Xcode 会与类似名称混淆,例如您无法将对象命名为“String”,它也会给出错误。我希望将来这会对某人有所帮助。

    【讨论】:

      【解决方案3】:

      此错误与 imageView 或 Button 或标签等对象的名称有关 修复此错误。只需将对象而不是 imageView 重命名为 restaurantImage 或将标签重命名为 RestaurantLabel 或将按钮重命名为 RestaurantButton 我希望你明白这一点。

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 2018-06-22
      • 1970-01-01
      • 2019-10-11
      相关资源
      最近更新 更多