Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of string literal for Objective-C selectors is deprecated, use '#selector' instead

I have the following code:

func setupShortcutItems(launchOptions: [NSObject: AnyObject]?) -> Bool {
    var shouldPerformAdditionalDelegateHandling: Bool = false

    if (UIApplicationShortcutItem.respondsToSelector("new")) {
        self.configDynamicShortcutItems()

        // If a shortcut was launched, display its information and take the appropriate action
        if let shortcutItem: UIApplicationShortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
            // When the app launched at the first time, this block can not called.
            self.handleShortCutItem(shortcutItem)

            // This will block "performActionForShortcutItem:completionHandler" from being called.
            shouldPerformAdditionalDelegateHandling = false
        } else {
            // normal app launch process without quick action
            self.launchWithoutQuickAction()
        }
    } else {
        // Less than iOS9 or later
        self.launchWithoutQuickAction()
    }

    return shouldPerformAdditionalDelegateHandling
}

I get the following "warning" on UIApplicationShortcutItem.respondsToSelector("new"), which says:

Use of string literal for Objective-c selectors is deprecated, use '#selector' instead

The warning replaces the code automatically with:

UIApplicationShortcutItem.respondsToSelector(#selector(FBSDKAccessToken.new))

However this doesn't compile because new() is unavailabe. What am I supposed to use in this case?

like image 625
fulvio Avatar asked Mar 23 '16 04:03

fulvio


People also ask

What are selectors Objective-C?

In Objective-C, selector has two meanings. It can be used to refer simply to the name of a method when it's used in a source-code message to an object. It also, though, refers to the unique identifier that replaces the name when the source code is compiled.

Why do we need to add @objc in the selector function?

That's where the @objc attribute comes in: when you apply it to a class or method it instructs Swift to make those things available to Objective-C as well as Swift code.


2 Answers

Xcode 7.3 using swift for iOS9.3/watchOS2.2/...

If you previously used this line of code:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateResult:", name: "updateResult", object: nil)

you should now use this line of code:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(InterfaceController.updateResult(_:)), name: "updateResult", object: nil)

at least this is what Xcode offered me after I changed around a few characters in the code. Seems like it doesn't always offer the correct solution when you are presented with this error.

like image 156
Florian Uhlemann Avatar answered Sep 17 '22 18:09

Florian Uhlemann


Create a protocol whose only reason for existing is to allow you to construct the appropriate selector. In this case:

@objc protocol NewMenuItemHandling {
  func new()
} 

You are taking the informal protocol (an object that responds to the new selector) and making it into a formal protocol.

Then where you want to use the selector you can add the expression:

#selector(NewMenuItemHandling.new)
like image 39
Scott Thompson Avatar answered Sep 19 '22 18:09

Scott Thompson