NSSelectorFromString and Selector Struct

511 Views Asked by At

I have a Question what is the different between NSSelectorFromString and Selector if i create the selector Using Selector

 let bSelector = Selector("registerRemoteNotificationWithApplication:")
 let cSelector = Selector(stringLiteral: "registerRemoteNotificationWithApplication:")

i get a warning

String literal is not a valid Objective-C selector

and when using the NSSelectorFromString

 let aSelector = NSSelectorFromString("registerRemoteNotificationWithApplication:")

there is no warning

even if the function was declared with / without @objc

  @objc func registerRemoteNotification(application:UIApplication) {


}
1

There are 1 best solutions below

0
Gereon On

Starting with Swift 3, you should use #selector() for this. Given

class Foo: NSObject {
    @objc func registerRemoteNotification(application:UIApplication) { }
}

you would use

let selector = #selector(Foo.registerRemoteNotification(application:))