Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 不推荐对Objective-C选择器使用字符串文字,请使用'#选择器';相反_Ios_Objective C_Iphone_Swift - Fatal编程技术网

Ios 不推荐对Objective-C选择器使用字符串文字,请使用'#选择器';相反

Ios 不推荐对Objective-C选择器使用字符串文字,请使用'#选择器';相反,ios,objective-c,iphone,swift,Ios,Objective C,Iphone,Swift,我有以下代码: func setupShortcutItems(launchOptions: [NSObject: AnyObject]?) -> Bool { var shouldPerformAdditionalDelegateHandling: Bool = false if (UIApplicationShortcutItem.respondsToSelector("new")) { self.configDynamicShortcutItems(

我有以下代码:

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(“新”)
上收到以下“警告”:

不推荐将字符串文字用于Objective-c选择器,请改用“#selector”

警告会自动将代码替换为:

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

但是,这不会编译,因为
new()
不可用。
在这种情况下,我应该使用什么?

在这种特殊的
respondsToSelector
情况下,如果您没有与函数引用关联的现有方法,请编写以下代码:

UIApplicationShortcutItem.respondsToSelector(Selector("new"))

您仍然会收到一个警告(您不应该收到,我已经针对它提交了一个bug),但它将是一个不同的警告,您可以忽略它。

创建一个协议,它存在的唯一原因是允许您构造适当的选择器。在这种情况下:

@objc protocol NewMenuItemHandling {
  func new()
} 
您正在使用非正式协议(响应新选择器的对象)并将其转换为正式协议

然后,可以在要使用选择器的位置添加表达式:

#selector(NewMenuItemHandling.new)

Xcode 7.3在iOS9.3/watchOS2.2/中使用swift

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

NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateResult:", name: "updateResult", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(InterfaceController.updateResult(_:)), name: "updateResult", object: nil)
您现在应该使用这行代码:

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

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

总之,每个“选择器:函数或对象”现在都是“选择器:#选择器(class.function(35;))。。。您可能有什么问题为什么要测试选择器
new
?我在这里没有看到任何发送
消息的代码。鬼鬼祟祟的。我不知道我会称之为鬼鬼祟祟。基本上,Objective-C很大程度上依赖于非正式协议(如果您愿意,也可以使用Duck类型)。斯威夫特希望在协议方面更正式一点。所以你已经采取了一个非正式的协议,并告诉斯威夫特它的存在:-)真正的笑话是,你应该能够通过说
#selector(NSObject.new)
来摆脱这个问题,因为这确实是你所说的
新的
。但是当你这么说的时候,你得到的是一个错误而不是警告@gotnull这是苹果公司对我的错误报告的回复:“工程部已经确定你的错误报告(24815319)是另一个问题(24791200)的副本,将被关闭。”请放心,继续往下说:副本越多越好。这对我来说更有效,比选择的答案更好。
按钮。addTarget(self,action:#选择器(ViewController.onClickStart),用于:.touchupinder)
我用这个。谢谢!@Florian Uhlemann