Introducing Swift Syntax and Data Types
Swift is a new programming language developed by Apple that was released to developers at WWDC 2014. Swift can be used to develop applications for Apple’s iOS and OS X platforms. It is designed to work with, but eventually replace, Objective-C, the language originally used for developing applications on Apple’s platforms.
Apple has a number of goals for Swift:
- Make app development easier, with modern language features.
- Produce safer code by preventing the most common programming errors.
- Create easy-to-read code with clear and expressive syntax.
- Be compatible with existing Objective-C frameworks, including the Cocoa and Cocoa Touch frameworks.
This chapter introduces the basic syntax of Swift and lays the foundation you’ll need for the rest of the book.
These are the key points in this chapter:
- You use var to declare a variable and let to declare a constant.
- You execute code conditionally with if or switch constructs.
- You repeat code segments by looping with for, for-in, while, and do-while loop constructs.
- The basic data types are implemented as structs, which are passed by value in code.
- Since the basic types are structs, they may have additional properties or methods available.
- Arrays and dictionaries in Swift are more powerful collection types than their Objective-C counterparts.
1.1 Basic Syntax
When you learn a new language, the first complete program you’re likely to see is the famous “Hello World” example. In Swift, this program consists of just one line:
println("Hello World")
The first thing you should notice here is what you don’t see. The code jumps right into the guts of the program. You don’t need to set anything up to get started, include or import a standard library, set up an initial main() function to be called by the system, or even include a semicolon at the end of each line.
1.1.1 Variables and Constants
A program that only prints a static line of text isn’t very useful. For a program to be useful, it needs to be able to work with data, using variables and constants. As their names imply, variables have contents that may change throughout the execution of the code, while constants cannot be changed after they’re initialized. Variables are said to be mutable; constants are immutable.
In Swift, you declare a variable by using the var keyword, and you declare a constant by using the let keyword. This applies for all data types in Swift and is different from Objective-C, where the type itself indicates whether it is mutable or not, such as NSArray versus NSMutableArray. With Swift, the mutable version of an object is the same type as the immutable version—not a subclass.
For the rest of this chapter, what we say about variables applies equally to constants (provided that we’re not talking about mutating data).
Swift is a strongly typed language, which means that every variable is set to a specific type at compile time, and it can only contain values of that type throughout its lifetime.
Two common types are Int and Float. (We’ll get into their details a little later.) If you set a variable to be of type Int, it can only ever store Int values; it can never be coerced into storing a Float. Types can never be implicitly converted into other types. This means, for example, that you cannot add an Int to a Float. If you need to add two numbers together, you need to make sure they’re the same type or explicitly convert one to the other. This is part of what makes Swift a safe language: The compiler prevents you from mixing types and possibly producing unexpected results.
To see the dangers involved in mixing types, consider this C code:
int intValue = 0; float floatValue = 2.5; int totalValue = intValue + floatValue;
This code adds an int and a float together. What would total be equal to here? Since the total is an int, it is unable to store the decimal portion of the floatValue variable. floatValue must first be implicitly converted to an int before it can be added to intValue and stored in totalValue. In this case, is the developer expecting the compiler to round floatValue to 3, or is she expecting it to just drop the decimal portion and instead add 2? Swift avoids this type of ambiguity by producing a compile-time error here, forcing you to tell it exactly what you want to happen. This is one way Swift avoids common programming errors.
You need to give variables and constants names so that you can refer to them in code. Names in Swift can be composed of most characters, including Unicode characters. While it’s possible to use emoji and similar characters as variable names, you should rarely, if ever, actually do it. Here is the minimum code for declaring a variable:
var itemCount: Int
This code declares a variable named itemCount of type Int. A variable must be set to an initial value before you can use it. You can do this when the variable is declared, like this:
var itemCount: Int = 0
or you can do it at some later point, as long as you do it before you attempt to read the value.
Swift has a feature called type inference. If the compiler has enough information from the initial value you set to infer the type, you can omit the type of the variable when you declare it. For example, if your variable is going to be an Int, you can declare it like this:
var itemCount = 0
Because 0 is an Int, itemCount is inferred to be an Int. This is exactly the same as the example above. The compiler generates exactly the same machine code.
If the variable’s initial value is set to the return value of a function, the compiler will infer the type to be the same as the return value’s type.
Given a function numberOfItems() that returns an Int and the following line:
var itemCount = numberOfItems()
the compiler will infer itemCount to be of type Int.
Since the compiler generates exactly the same code whether you explicitly set the type or use type inference to let the compiler set the type for you, there is no advantage or disadvantage to either method at run time. Of course, if you need to explicitly set the type, you have no option. But in cases where the compiler can infer the type, it’s up to you whether to let the compiler do so or whether you explicitly set the type anyway. There are two things to consider when making this decision. The first is readability. If, when you use type inference, the type of the variable would still be clear to a future reader of the code, by all means save some keystrokes and use type inference. If the initial value being set is the return value of some uncommon function, it may be clearer to the future reader if you explicitly set the type. When reading the code at a later date, you don’t want to have to look up what a function returns just to determine a variable’s type.
The second reason you might want to explicitly set a type when it can be inferred is to add an additional safety check. This ensures that the type you’re expecting the variable to be and the type being set match. If there’s a mismatch, you get a compile-time error and can make the necessary corrections.
1.1.2 String Interpolation
You’ve already seen how to print a line of text to the console by using the println command. You can add variables, constants, and other expressions to the output by using string interpolation. You do so by including variables and expressions directly in the string literal, surrounded by parentheses and escaped with a backslash:
var fileCount = 99 println("There are \(fileCount) files in your folder") //outputs: There are 99 files in your folder
This doesn’t apply just to println. You can use it anywhere a string literal is used:
var firstName = "Geoff" var lastName = "Cawthorne" var username = "\(firstName)\(lastName)\(arc4random() % 500)" //username: GeoffCawthorne253
1.1.3 Control Flow
All but the simplest of programs require some sort of logic to determine what actions should be taken. Decisions must be made based on the information the program has available. Logic such as “If this, do that” or “Do this x many times” determines the flow of an app and, thus, its result.
Conditionals
Swift offers both if and switch constructs for you to execute code conditionally.
Using if is the simpler of the two constructs and closely follows what you’re used to in Objective-C. There are a few differences you need to be aware of, however. The first difference continues Swift’s theme of reducing unnecessary syntax: Swift does not require you to surround test expressions with parentheses, though you may, if you desire. The second difference is that braces are required around the conditional code. Third, the test expression must explicitly result in a true or false answer; an Int variable with a value of 0 is not implicitly evaluated as false, nor is a value of anything else implicitly evaluated as true.
Here is a minimal example:
var daysUntilEvent: Int = calculateDaysUntilEvent() if daysUntilEvent > 0 { println("There is still time to buy a gift") }
You can chain together multiple ifs with the else keyword:
var daysUntilEvent: Int = calculateDaysUntilEvent() if daysUntilEvent > 0 { println("There is still time to buy a gift") } else if daysUntilEvent < 0 { println("You missed it, better pick up a belated card") } else { println("Better pick up the gift on the way") }
The switch construct is an alternative to if statements. It is based on what you’ve used in Objective-C, but in Swift, it is much more powerful. There are two important differences you need to consider when using a switch in Swift. The first is that every possible option must be covered. A default case can be used to accomplish this requirement. The second difference is a major change in how cases are handled. In C-based languages, you need to include a break statement at the end of each case, or execution will continue with the next case. This has been the source of many errors over time. To prevent these errors in Swift, the design was changed to automatically break when the next case begins. Some algorithms may require the old behavior, so it is available to you through the use of the fallthrough keyword.
Here’s a basic example of a switch in use:
var numberOfItemsInCart: Int = calculateNumberOfItemsInCart() switch numberOfItemsInCart { case 0: println("Cart is Empty") case 1: println("1 item in cart, standard shipping applies") default: println("\(numberOfItemsInCart) items, you quality for free shipping") }
We’ll cover advanced switch usage in Chapter 2, “Diving Deeper into Swift’s Syntax.”
Loops
In Swift, for, for-in, while, and do-while are used for looping. These are similar to what you’re used to in Objective-C, with only slight differences in the syntax.
Here is a basic for example:
for var i = 0; i < 10; ++i { println("i = \(i)") }
Just as with if statements, you can omit the parentheses. In this example, i is implicitly declared as an Int. The loop will iterate while i < 10, and it’s incremented by 1 at the end of each iteration.
Another form of the for loop is the for-in loop. This lets you iterate through each item in a collection, such as an array or a range.
Swift has two new range operators for creating ranges that can be used with for-in loops. The ..< operator is the half-open range operator; it includes the value on the left side but not the value on the right side. Here’s an example that iterates 10 times, with i starting as 0 and ending as 9:
for i in 0 ..< 10 { println("i = \(i)") }
The ... operator is the inclusive range operator. It includes the values on both sides. This example iterates 10 times, with i starting as 1 and ending as 10:
for i in 1 ... 10 { println("i = \(i)") }
When you use a for-in loop to iterate through a collection such as an Array, it looks like this:
var itemIds: [Int] = generateItemIds() for itemId in itemIds { println("itemId: \(itemId)") }
The while loop iterates for as long as the test condition is true. If the test condition is false at the start, the loop doesn’t iterate at all, and it is just skipped entirely. For instance, this example will never actually print 100% complete since the test condition becomes false once percentComplete == 100:
var percentComplete: Float = calculatePercentComplete() while percentComplete < 100 { println("\(percentComplete)% complete") percentComplete = calculatePercentComplete() }
If you change this to a do-while loop, the test condition is evaluated at the end of the loop. This guarantees that the loop will iterate at least once and also means it will iterate a final time when the test condition fails (which could be the first iteration). This version updates the display one final time once the task being monitored is complete:
var percentComplete: Float = 0.0 do { percentComplete = calculatePercentComplete() println("\(percentComplete)% complete") } while percentComplete < 100
When using a loop, there are times when you need to adjust the iterations by either quitting iteration altogether or skipping a single iteration. Just as in Objective-C, there are two keywords you can use for these purposes: break and continue. You use break to immediately jump out of the loop and cancel any further iterations:
var percentComplete: Float = 0.0 do { percentComplete = calculatePercentComplete() if taskCancelled() { println("cancelled") break } println("\(percentComplete)% complete") } while percentComplete < 100
You use continue to end the current iteration and immediately start the next one:
var filesToDownload: [SomeFileClass] = filesNeeded() for file in filesToDownload { if file.alreadyDownloaded { continue } file.download() }
With nested loops, break and continue affect only the inner loop. Swift has a powerful feature that Objective-C does not have: You can add labels to your loops and then specify which loop you would like to break or continue out of. A label consists of the name followed by a colon in front of the loop keyword:
var folders: [SomeFolderClass] = foldersToProcess() outer: for folder in folders { inner: for file in folder.files { if shouldCancel() { break outer } file.process() } }