gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:22:48 27 4
gpt4 key购买 nike

我有以下代码:

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
}

我在 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(_:)), name: "updateResult", object: nil)

至少这是 Xcode 在我更改代码中的几个字符后提供给我的。当您遇到此错误时,它似乎并不总是提供正确的解决方案。

关于ios - 不推荐使用 Objective-C 选择器的字符串文字,改用 '#selector',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36170049/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com