【问题标题】:Cannot override mutable property 'refreshControl' of type 'UIRefreshControl?' with covariant type 'UIRefreshControl'无法覆盖“UIRefreshControl”类型的可变属性“refreshControl”?具有协变类型“UIRefreshControl”
【发布时间】:2018-01-17 11:03:00
【问题描述】:

我在表格视图中使用UIRefreshControl 来提取新数据。但是当我定义控件时,会显示以下错误。谷歌搜索了一段时间,但没有发现任何有用的东西。

我使用 Xcode 版本 9.2 (9C40b)、swift4、IOS11.2

无法覆盖类型的可变属性“refreshControl” 'UIRefreshControl?'具有协变类型“UIRefreshControl”

var refreshControl = UIRefreshControl()

【问题讨论】:

    标签: ios swift uitableview uirefreshcontrol


    【解决方案1】:

    如果要定义另一个控件,请更改名称...refreshControlUIScrollView/UITableViewController ivar,因为 iOS10/iOS6 [分别]。您正在尝试重新定义它,这就是导致错误的原因。当然,您也可以使用提供的。

    您的意思可能类似于self.refreshControl = UIRefreshControl()

    【讨论】:

      【解决方案2】:

      斯威夫特 4/5

      override func viewDidLoad() {
          super.viewDidLoad()
      
          self.refreshControl = UIRefreshControl()
          self.refreshControl!.attributedTitle = NSAttributedString(string: "Pull to refresh")
          self.refreshControl!.addTarget(self, action: #selector(refresh(sender:)), for: UIControl.Event.valueChanged)
      
      }
      
      @objc func refresh(sender:AnyObject) {
         // Code to refresh table view
      
          self.refreshControl!.endRefreshing()
      }
      

      【讨论】:

        【解决方案3】:

        编译器阻止您在子类中指定比超类设置的要求更严格的要求。

        假设编译器允许您使用非Optional 类型覆盖Optional 属性。如果有人这样做会发生什么:

        let tableView: UITableView = CoolTableViewSubclass()
        tableView.refreshControl = nil // but my overridden property can't be nil!
        

        作为一个子类,当有人像对待你的超类一样对待你时,你需要好好工作。所以在这种情况下编译器会阻止你,因为它已经可以告诉你你违反了这个规则。

        Mike Ash wrote this up in an unsurprisingly good post if you're curious.

        【讨论】:

          猜你喜欢
          • 2020-12-08
          • 1970-01-01
          • 2019-06-12
          • 1970-01-01
          • 2013-04-05
          • 1970-01-01
          • 1970-01-01
          • 2015-04-01
          • 1970-01-01
          相关资源
          最近更新 更多