微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – `#selector`的参数不是指初始值设定项或方法

我正在尝试在后台执行协议扩展方法

performSelectorInBackground(#selector(retrieveCategories()),withObject: nil)

但是我收到以下错误消息:

Argument of `#selector` does not refer to an initializer or method

这是我的协议声明:

@objc protocol DataRetrievalOperations {
    optional func retrieveCategories()
    ...
}

我的扩展:

extension DataRetrievalOperations {
    func retrieveCategories() {
        ...
    }
}

我怎样才能做到这一点?

解决方法

您无法在协议扩展中添加@Objc方法.您需要扩展继承NSObject和该协议的Class并在其中添加objc函数,如下所示:

@objc protocol DataRetrievalOperations {
    optional func retrieveCategories()
}

class aClass: NSObject,DataRetrievalOperations {
    func method() {
        performSelectorInBackground(#selector(retrieveCategories),withObject: nil)
    }
}

extension aClass {
    @objc func retrieveCategories(){

    }
}

这会奏效.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐