Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the advantage of #selector update in Swift 2.3?

Tags:

swift

I can't see a problem with just writing the method name as a string. Im just curious as to why this is better?

like image 453
Craig Avatar asked Mar 24 '16 22:03

Craig


People also ask

What are some examples of advantage?

The definition of advantage means anything that provides a more favorable position, greater opportunity or a favorable outcome. An example of an advantage is when a football team plays a game in their home stadium. The first point scored in tennis after deuce. To provide (someone) with an advantage, to give an edge to.

What does advantage mean?

1 : something that benefits the one it belongs to Speed is an advantage in sports. 2 : the fact of being in a better position or condition His great height is an advantage in basketball. 3 : personal benefit or gain It's to your own advantage to study.

What is the benefit of advantage?

advantage: something (such as a good position or condition) that helps to make someone or something better or more likely to succeed than others. a good or desirable quality or feature. benefit or gain.


3 Answers

This is a huge change. Basically, this closes the biggest crash-hole in the language.

If you form a Selector as a string literal, and you form it wrong — which is all too easy — or if you form it right but the method in question is not exposed to Objective-C, you will crash at runtime with the dreaded Unrecognized selector console message — the most common crash in both Objective-C and Swift. (Do a Stack Overflow on "unrecognized selector"; you will see what I mean.)

Now, the #selector syntax means that you will form the Selector using a function reference, which the compiler will check at compile time. If you make a mistake, the compiler stops you dead. If you do it right, the compiler forms the Selector for you — correctly. You don't need to know anything about how to form the Selector string; the compiler does all the work. Thus, your chance of crashing in this way is effectively reduced to zero. The "unrecognized selector" crash is dead as a doornail.

like image 136
matt Avatar answered Sep 22 '22 15:09

matt


The #selector update lets you use autocomplete. When using string literals, you could add a typo which would create a run-time error.

Just to add, when using the migration tool in Xcode, Xcode will change your selector to something like:

#selector(MyController.myMethod)

It's acceptable to remove the class' name which makes it a little cleaner, like so:

#selector(myMethod)
like image 45
Hunter Monk Avatar answered Sep 26 '22 15:09

Hunter Monk


“Before Swift 2.2, selectors were string literals and prone to error because we, as humans invented, and still contribute to typos whenever given the chance to write something without autocomplete.”

https://medium.com/swift-programming/swift-selector-syntax-sugar-81c8a8b10df3#.8ki9dd38j

like image 23
Zelko Avatar answered Sep 23 '22 15:09

Zelko