Apple Swift programming language has this amazing feature that you can use the reserved keywords of the programming language as your variable, constant, name of the class or whatsoever. To do that, all you need to do is surround the reserved keyword with a backtick, as shown below.
var = "Hello"[/sourcecode]
The above code creates a constant ‘var’ of String type having the “Hello” in it. Since Swift has this feature to infer the variable or constant automatically by detecting the kind of value going in it, it doesn’t require you to specify the type except in situations when you are sure about type of value of going in it. By ensuring the type you can be more precise with your program and avoid any overflow situations.
Another example of it would be:
[sourcecode language=”JS”] var
var = 123 [/sourcecode]
Here again we have declared a variable ‘var’ with the same name as the reserved Swift keyword.
Now let’s take a closer look into the situations where we can make some mistakes while using the reserved Swift keywords.
The very first mistake would be misinterpreting the backticks (`) [You can found this symbol just below the tilt operator “~” near F1 key] with an inverted comma (‘). Try to avoid it or you will end up with such errors:
[sourcecode language=”JS”] let ‘var’ = "Hello" [/sourcecode]
[sourcecode language=”JS”]error: Expected Pattern [/sourcecode]
[sourcecode language=”JS”]error: Invalid character in source file [/sourcecode]
Another common mistake would be using the reserved keywords without any backticks at all and end up with such errors:
[sourcecode language=”JS”] let var = "Hello" [/sourcecode]
[sourcecode language=”JS”]error:’var’ cannot appear nested inside another ‘var’ or ‘let’ pattern[/sourcecode]
Meaning, you cannot use them like this. Even though Swift has this feature, it doesn’t mean that you should always use it. You will always end up with poor code and EdBinx suggests you to not to use it and avoid as much as you can.
MacBook Pro mid-2014
El Capitan Beta 5