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?

1

1 Answer 1

2

This feature effectively deprecates using Selector("methodName") and also just using "methodName" or "methodName:" for selectors.

The main benefit is that you can't make typos in the method string anymore, as you already state in the question.

Imagine a method with a selector:

..., selector: "myMethod:")

What happens when you typo?

..., selector: "mymethod:")

It crashes.

With the new system it's type-safe: the compiler can check that the method you're calling actually exists - no more typos, no more calling non-existing functions:

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

because the compiler can check the types. We also get auto-suggestion and auto-completion from Xcode and all the niceties coming with type-safe operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.