ios – 不推荐使用Objective-C选择器的字符串文字,请改用’#selector’

我有以下代码
func setupShortcutItems(launchOptions: [NSObject: AnyObject]?) -> Bool {
    var shouldPerformAdditionalDelegateHandling: Bool = false

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

        // If a shortcut was launched,display its @R_500_4045@ion 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
}

我在UIApplicationShortcutItem.respondsToSelector(“new”)上得到以下“警告”,它说:

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

警告会自动替换代码

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

但是,由于new()不可用,因此无法编译.
在这种情况下我应该用什么?

解决方法

Xcode 7.3使用swift for iOS9.3 / watchOS2.2 / …

如果您以前使用过这一行代码

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

您现在应该使用这一行代码

NSNotificationCenter.defaultCenter().addobserver(self,selector: #selector(InterfaceController.updateResult(_:)),object: nil)

至少这是Xcode在代码中改变了几个字符后提供给我的.看起来似乎并不总是提供正确的解决方案,当你出现这个错误.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...