理解Swift 2.2选择器语法 - #selector()

时间:2016-03-22 22:14:30

标签: swift swift2 selector

我正在将我的项目语法切换到Swift 2.2(xCode帮助我自动完成);但是,我不理解新的#selector()语法。

举个例子:

timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, 
             selector: #selector(MyVC.timerCalled(_:)), //new selector syntax!
             userInfo: nil, repeats: true)

这有选择器#selector(MyVC.timerCalled(_:))

_:表示什么?你能在这个选择器中添加其他变量吗?说,#MyVC.timerCalled(_:whateverVar)

非常感谢有关此语法的不同内容的一般信息,而不是早期版本的Swift中基于字符串的实现。

4 个答案:

答案 0 :(得分:31)

括号中的位是一种用于标识所需选择器的参数列表的机制。

我建议您查看Swift Evolution的Generalized Naming提案。它涵盖了一些情况,其中有许多功能仅由参数标签不同而需要引用它们。该文件的例子是:

extension UIView {
  func insertSubview(view: UIView, at index: Int)
  func insertSubview(view: UIView, aboveSubview siblingSubview: UIView)
  func insertSubview(view: UIView, belowSubview siblingSubview: UIView)
}

如果你想获得其中一个的函数值,结果是不明确的:

let fn = someView.insertSubview // ambiguous: could be any of the three methods

实现的解决方案是将参数标签添加到生成函数值的代码中,而不添加任何类型信息以消除您想要的歧义:

let fn = someView.insertSubview(_:at:)
let fn1 = someView.insertSubview(_:aboveSubview:)

了解如何在parens中添加标签?

此提案在最直接适用于您的问题的提案中发挥了作用:

Referencing the Objective-C selector of a method

在这种特殊情况下,您要引用的选择器是timerCalled:,它是一个没有标签的参数的函数。因此(_ :)。下划线表示未指定标签和冒号。

答案 1 :(得分:19)

Swift 2.2已弃用字符串化选择器:在swift 2.0中,我们使用将选择器编写为字符串,即"buttonClicked"。这种方法的缺点是编译器无法在编译时检查方法是否真的存在(即使你拼错了它)。

<强> EX:1

func buttonClicked(){
}

因此,新方法中的上述方法可以称为#selector(buttonClicked)

<强> EX:2

func buttonClicked(stringValue : String){
}

因此,新方法中的上述方法可以称为#selector(buttonClicked(_:))

<强> EX:3

func buttonClicked(stringValue : String, indexValue : Int){
}

因此,上述带有新方法参数的方法可以称为#selector(buttonClicked(_:indexValue:))

答案 2 :(得分:9)

使用#selector

考虑下面的代码,用于在swift 3中将按钮添加到按钮
button.addTarget(self, action: #selector(self.buttonAction(sender:)),
                       for: UIControlEvents.touchUpInside)

 func buttonAction(sender:UIButton!){

 }

迁移到swift 3时,此语法对我有用

答案 3 :(得分:8)

这是Swift方法签名在文档中的表示方式,现在它开始用于新的语言功能,例如#selector()语法,通过参数列表表达方法。

每个冒号(:)表示方法参数。对于命名参数,冒号前面是外部参数名称;对于未命名的参数,使用下划线(_)。

例如,MyVC.timerCalled(_:))表示MyVC类型的方法,其中包含一个未命名的参数,可以这样声明:

func timerCalled(timer: NSTimer) { ... }

(请注意,timer内部参数名称,因为默认情况下,方法的第一个参数是未命名的)

如果类型(示例中为MyVC)与#selector()声明的范围相同,也可以省略。

更复杂的示例可能如下所示:

let sel = #selector(aMethodWithSeveralParameters(_:secondParam:thirdParam:))

...

func aMethodWithSeveralParameters(firstParam: Int, secondParam: Int, thirdParam: Int) { ... }
相关问题