Can Sürmeli Can Sürmeli - 5 months ago 694
Swift Question

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

I'm trying to perform a protocol extension method in the background:

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


However I get the below error message:

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


Here is my protocol declaration:

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


And my extension:

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


How can I achieve this?

Answer

You can't add an @Objc method in a protocol extension. You need to extend the class which inherits NSObject and that protocol and add the objc function there like so:

@objc protocol DataRetrievalOperations {
    optional func retrieveCategories()

}




class aClass: NSObject, DataRetrievalOperations{


    func method(){
        performSelectorInBackground(#selector(retrieveCategories), withObject: nil)
    }

}

extension aClass{

    @objc func retrieveCategories(){

    }

}

This will work.

Comments