Wednesday, November 2, 2016

A Swift Tour

A Swift Tour

Tradition suggests that the first program in a new language should print the words “Hello, world!” on the screen. In Swift, this can be done in a single line:



If you have written code in C or Objective-C, this syntax looks familiar to you—in Swift, this line of code is a complete program. You don’t need to import a separate library for functionality like input/output or string handling. Code written at global scope is used as the entry point for the program, so you don’t need a main() function. You also don’t need to write semicolons at the end of every statement.

This tour gives you enough information to start writing code in Swift by showing you how to accomplish a variety of programming tasks. Don’t worry if you don’t understand something—everything introduced in this tour is explained in detail in the rest of this book.

NOTE

On a Mac, download the Playground and double-click the file to open it in Xcode: https://developer.apple.com/go/?id=swift-tour


Simple Values

Use let to make a constant and var to make a variable. The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once. This means you can use constants to name a value that you determine once but use in many places.



A constant or variable must have the same type as the value you want to assign to it. However, you don’t always have to write the type explicitly. Providing a value when you create a constant or variable lets the compiler infer its type. In the example above, the compiler infers that myVariable is an integer because its initial value is an integer.

If the initial value doesn’t provide enough information (or if there is no initial value), specify the type by writing it after the variable, separated by a colon.



Values are never implicitly converted to another type. If you need to convert a value to a different type, explicitly make an instance of the desired type.



There’s an even simpler way to include values in strings: Write the value in parentheses, and write a backslash (\) before the parentheses. For example:



Create arrays and dictionaries using brackets ([]), and access their elements by writing the index or key in brackets. A comma is allowed after the last element.



To create an empty array or dictionary, use the initializer syntax.


If type information can be inferred, you can write an empty array as [] and an empty dictionary as [:]—for example, when you set a new value for a variable or pass an argument to a function.



Control Flow

Use if and switch to make conditionals, and use for-in, for, while, and repeat-while to make loops. Parentheses around the condition or loop variable are optional. Braces around the body are required.


In an if statement, the conditional must be a Boolean expression—this means that code such as if score { ... } is an error, not an implicit comparison to zero.

You can use if and let together to work with values that might be missing.

These values are represented as optionals. An optional value either contains a value or contain nil to indicate that a value is missing. Write a question mark (?) after the type of a value to mark the value as optional.


 If the optional value is nil, the conditional is false and the code in braces is skipped. otherwise, the optional value is unwrapped and assigned to the constant after let, which makes the unwrapped values available inside the block of code.
Another way to handle optional values is to provide a default values using the ?? operator . If the optional value i missing, the default values is used instead.



Switchers support any kind of data and a wide variety of comparison operations -- they aren't limited to integers and tests for equality.



Notice how let can be used in a pattern to assign the value the matched the pattern to a constant.

After executing the code inside the switch case that matched, the program exits from the the switch statement. Exception doesn't continue to the next case, so there is no need to explicitly break out of the switch at the end of each case's code.

You use for-in to iterate over items in a dictionary by providing a pair of the names to use for each key-value pair. Dictionaries are an unordered collection, so their keys and values are iterated over in an arbitrary order.


Use while to repeat a block of code until a condition changes. The condition of a loop can be at the end instead, ensuring that the loop is run at least once.



You can keep an index in a loop by using ..< to make a range of indexes.



Use ..< to make a range that omits its upper value, and use ... to make a range that includes both values.