Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Swift

You're reading from   Mastering Swift

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher Packt
ISBN-13 9781784392154
Length 358 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jon Hoffman Jon Hoffman
Author Profile Icon Jon Hoffman
Jon Hoffman
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Taking the First Steps with Swift FREE CHAPTER 2. Learning about Variables, Constants, Strings, and Operators 3. Using Collections and Cocoa Data Types 4. Control Flow and Functions 5. Classes and Structures 6. Working with XML and JSON Data 7. Custom Subscripting 8. Using Optional Type and Optional Chaining 9. Working with Generics 10. Working with Closures 11. Using Mix and Match 12. Concurrency and Parallelism in Swift 13. Swift Formatting and Style Guide 14. Network Development with Swift 15. Adopting Design Patterns in Swift Index

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:

Comments

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:

Comments

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:

Semicolons

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:

Parentheses

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:

Parentheses

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:

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:

Assignment operator (=) does not return a value

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.

Spaces are optional in conditional and assignment statements

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.

You have been reading a chapter from
Mastering Swift
Published in: Jun 2015
Publisher: Packt
ISBN-13: 9781784392154
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image