Swift language syntax
If you are an Objective-C developer, and you are not familiar with modern languages like Python or Ruby, the code in the previous screenshots probably looked pretty strange. The Swift language syntax is a huge departure from Objective-C, which was based largely on Smalltalk and C.
The Swift language uses very modern concepts and syntax to create very concise and readable code. There was also a heavy emphasize on eliminating common programming mistakes. Before we get into the Swift language itself, let's take a look at some of the basic syntax of the Swift language.
Comments
Writing comments in Swift code is a little different from writing comments in Objective-C code. We still use the double slash //
for single line comments and the /*
and */
for multiline comments.
What has changed is how we document the parameters and the return value. To document any parameter, we use the :parm:
field, and for the return value, we use the :return:
field. The following Playground shows examples of both single line and multiline comments to properly comment a function:
To write good comments, I would recommend using single line comments within a function to give quick one-line explanations of your code. We will then use the multiline comments outside of functions and classes to explain what the function and class does. The preceding Playground shows a good use of comments. By using proper documentation, as we did in the preceding screenshot, we can use the documentation feature within Xcode. If we hold down the option key, and then click on the function name anywhere in our code, Xcode will display a popup with the description of the function. This next screenshot shows what that popup would look like:
Semicolons
You probably noticed from the code samples so far, that we are not using semicolons at the end of lines. The semicolons are optional in Swift; therefore, both lines in the following Playground are valid in Swift. You can see the results of the code in the results sidebar, as shown in the following screenshot:
For style purposes, it is strongly recommended that you do not use semicolons in your Swift code. If you are really set on using semicolons in your code (I would not recommend it), then I would recommend just being consistent and using it on every line of code; however, Swift will not warn you if you forget them.
Parentheses
In Swift, parentheses around conditional statements are optional, for example, both if statements in the following Playground are valid. You can see the results of the code in the sidebar, as shown in the following screenshot:
For style purposes, it is recommended that you do not include the parentheses in your code unless you have multiple conditional statements on the same line. For readability purposes, it is good practice to put parentheses around the individual conditional statements that are on the same line. See the following Playground for samples:
Curly braces
In Swift, unlike most other languages, the curly bracket is required after statements. This is one of the safety features that are built into Swift. Arguably, there have been numerous security bugs that may have been prevented if the developer would have used curly braces. A good example of this is Apple's Goto Fail
bug. These bugs could also have been prevented by other means, as well as unit testing and code reviews, but requiring developers to use curly braces, in my opinion, is a good security standard. The following Playground shows you what error you get if you forget to include the curly braces:
Assignment operator (=) does not return a value
In most other languages, the following line of code is valid, but it probably is not what the developer meant to do:
if (x =1) { }
In Swift, this statement is not valid. Using an assignment operator (=
) in a conditional statement (if
and while
) will throw an error. This is another safety feature built into Swift. It prevents the developer from forgetting the second equals sign (=
) in a comparison statement. This error is shown in the following Playground:
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Spaces are optional in conditional and assignment statements
For both conditional (if
and while
) and assignment (=
) statements, the white spaces are optional. Therefore, in the following Playground, both the i block
and j block
of code are valid.
Tip
For style purposes, I would recommend adding the white spaces (such as the j block
for readability purposes), but as long as you pick one style and be consistent, either style should be acceptable.