我正在尝试在后台执行协议扩展方法: performSelectorInBackground(#selector(retrieveCategories()), withObject: nil) 但是我收到以下错误消息: Argument of `#selector` does not refer to an initializer or method 这是我
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(){ } }
这会奏效.