首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实例方法'widgetPerformUpdate(completionHandler:)‘几乎匹配可选的需求'widgetPerformUpdate(completionHandler:)’

实例方法'widgetPerformUpdate(completionHandler:)‘几乎匹配可选的需求'widgetPerformUpdate(completionHandler:)’
EN

Stack Overflow用户
提问于 2016-10-21 01:25:52
回答 1查看 703关注 0票数 1

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

实例方法'widgetPerformUpdate(completionHandler:)‘几乎匹配协议'NCWidgetProviding’的可选需求'widgetPerformUpdate(completionHandler:)‘

代码语言:javascript
复制
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)
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-27 04:08:06

在参数类型之前写入@escaping,以指示闭包允许转义。

代码语言:javascript
复制
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)
}

该函数基本上以闭包参数作为完成处理程序。函数在开始操作后返回,但在操作完成后才会调用闭包--闭包需要转义,稍后才调用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40166939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档