2

As it possible to conclude in swift 2.2 version it will be possible to reference to the Objective-C method via #selector.

let sel = #selector(UIView.insertSubview(_:at:)) // produces the Selector "insertSubview:atIndex:"

So previously we wear using name of the method like simple string : "doBangBang" and call it Selector("doBangBang") and now we should use it like reference to the method MyClass.doBangBang() and with usage of key word #selector(MyClass.doBangBang())? Does this feature deprecate Selector? And what benefits are from this improvments except of reducing amount of the functions that were perform with wrong name?

4

1 回答 1

2

此功能有效地弃用了使用或Selector("methodName")仅使用"methodName""methodName:"用于选择器。

主要好处是您不能再在方法字符串中输入拼写错误,正如您在问题中已经说明的那样。

想象一个带有选择器的方法:

..., selector: "myMethod:")

当你打错字时会发生什么?

..., selector: "mymethod:")

它崩溃了。

使用新系统,它是类型安全的:编译器可以检查您调用的方法是否确实存在 - 不再有拼写错误,不再调用不存在的函数:

..., selector: #selector(myMethod))

因为编译器可以检查类型。我们还从 Xcode 获得自动建议和自动完成功能,以及类型安全操作带来的所有细节。

于 2016-03-15T09:29:56.560 回答