Xcode 7.3/Swift 2:“没有使用 Objective-C 选择器声明的方法"警告 [英] Xcode 7.3 / Swift 2: "No method declared with Objective-C selector" warning

查看:39
本文介绍了Xcode 7.3/Swift 2:“没有使用 Objective-C 选择器声明的方法"警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用选择器,即使在迁移到 Swift 之后,我也能够毫无问题地使用它们.这就是我在 Swift 2 上使用的方式,直到我将 Xcode 更新到 7.3 版:

可以看到我使用 NSTimer 的选择器.

这是被调用的动作:

 func Start() {}

正如你所看到的,Xcode 7.3 现在给出了一个警告没有使用 Objective-C 选择器声明的方法".通过单击警告,Xcode 通过添加选择器"提供对代码的快速修复,但随后我仍然收到相同的警告:

解决方案

从 Swift 2.2/Xcode 7.3 开始,有一种使用选择器的新方法:Selector("funcName") 改为 #selector(ClassName.funcName)

看看

它会给你建议:

如果您选择该建议,它会自动更新选择器:

I've been using selectors for a while, and even after migrating to Swift I was able to use them without issues. That's how I was using on Swift 2 without issues until I updated Xcode to version 7.3:

As use can see I use selectors with NSTimer.

This is the action that is called:

 func Start () {

 }

As you can see Xcode 7.3 now gives a warning "No method declared with Objective-C selector". By clicking on the warning, Xcode offers a quick fix to the code by adding "Selector", but then I still get the same warning:

解决方案

Since Swift 2.2 / Xcode 7.3 there is a new way to use a selector: Selector("funcName") was changed to #selector(ClassName.funcName)

Have a look at https://github.com/apple/swift-evolution/blob/master/proposals/0022-objc-selectors.md ,

tl;dr;

Replace Selector("Start") with #selector(YOUR_CLASS.Start)

where YOUR_CLASS = class of target in given context.

If you don't want to do it manually, Xcode provides easy fix itself by default, when you have the following situation, tap on the Yellow triangles ( sometimes required to tap/click multiple times ),

it will give you suggestion:

And if you select that suggestion, it will automatically update the selector:

这篇关于Xcode 7.3/Swift 2:“没有使用 Objective-C 选择器声明的方法"警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆