I can't see a problem with just writing the method name as a string. Im just curious as to why this is better?
-
A related question: http://stackoverflow.com/q/36184626/1445366 – Aaron Brager Mar 24 '16 at 22:39
-
That is not the same question. – Craig Mar 24 '16 at 23:00
-
2I said it was *related*, not a duplicate. Just thought you might find it interesting. – Aaron Brager Mar 25 '16 at 03:16
-
Really? You never had a crash because you mistyped the string? – crashoverride777 Mar 25 '16 at 12:18
-
2You're joking right? – TheCodingArt Jun 23 '16 at 18:29
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.

- 515,959
- 87
- 875
- 1,141
-
Any reason Apple is still keeping the old syntax? I mean in Swift they keep changing syntaxes and **get rid of the old syntax**. Does old form have any usage? – mfaani Oct 05 '16 at 16:40
-
-
sure, my question was where is it needed? Where is it that the better syntax ie `#selector` doesn't work? – mfaani Oct 05 '16 at 16:48
-
@Honey If you ask it as an actual question I'll be happy to answer it with an actual answer. – matt Oct 05 '16 at 16:59
-
I just found its answer [here](http://stackoverflow.com/questions/24007650/selector-in-swift?rq=1) sorry for the annoyance – mfaani Oct 05 '16 at 17:05
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)

- 1,967
- 1
- 14
- 25
“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

- 3,793
- 3
- 34
- 40
-
6Not only autocomplete, but most importantly there will be a compile-time error if you made a mistake, rather than a run-time error. – Tim Vermeulen Mar 24 '16 at 22:37
-
I always copy-paste to avoid all errors. create your method first then copy the name while your there, scroll up find the spot, cmd v, done. There has to be another reason other than to help us avoid error. – Craig Mar 24 '16 at 23:06
-
1@Craig your method does not have the same security the new #selector syntax provides. What if you find yourself refactoring method names later and forget to rename the associated selector calls? – Andy Ibanez Mar 24 '16 at 23:22