【问题标题】:Instance method 'widgetPerformUpdate(completionHandler:)' nearly matches optional requirement 'widgetPerformUpdate(completionHandler:)'实例方法“widgetPerformUpdate(completionHandler:)”几乎匹配可选要求“widgetPerformUpdate(completionHandler:)”
【发布时间】:2016-10-21 01:25:52
【问题描述】:

我是今天扩展的新手,我收到了这个警告,有人知道如何匹配可选要求吗?

实例方法“widgetPerformUpdate(completionHandler:)”几乎匹配协议“NCWidgetProviding”的可选要求“widgetPerformUpdate(completionHandler:)”

func widgetPerformUpdate(completionHandler: ((NCUpdateResult) -> Void)) {
    // Perform any setup necessary in order to update the view.
    // If an error is encountered, use NCUpdateResult.Failed
    // If there's no update required, use NCUpdateResult.NoData
    // If there's an update, use NCUpdateResult.NewData

    let result = performFetch()
    if result == .newData{
        tableView.reloadData()
        self.preferredContentSize = tableView.contentSize
    }
    completionHandler(result)
}

【问题讨论】:

  • 试试func widgetPerformUpdate(completionHandler: @escaping (NCUpdateResult) -> Void) {
  • @rmaddy,它有效!

标签: ios swift xcode swift3 today-extension


【解决方案1】:

在参数类型前写@escaping表示允许闭包逃逸。

func widgetPerformUpdate(completionHandler: (@escaping(NCUpdateResult) -> Void)) {
    // Perform any setup necessary in order to update the view.
    // If an error is encountered, use NCUpdateResult.Failed
    // If there's no update required, use NCUpdateResult.NoData
    // If there's an update, use NCUpdateResult.NewData

    let result = performFetch()
    if result == .newData{
        tableView.reloadData()
        self.preferredContentSize = tableView.contentSize
    }
    completionHandler(result)
}

这个函数基本上接受一个闭包参数作为完成处理程序。函数在开始操作后返回,但在操作完成之前不会调用闭包——闭包需要转义,稍后再调用。

【讨论】:

    猜你喜欢
    • 2017-12-24
    • 2018-06-23
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多