Mastering Swift 5
Rate it:
9%
Flag icon
if we want to use the comments to also document our code, we need to use the triple slash (///) or multiline comment block.
9%
Flag icon
There are significantly more fields that we can add to our comments. You can find the complete list on Apple's site: https://developer.apple.com/library/
10%
Flag icon
For style purposes, it is strongly recommended that you do not use semicolons in your Swift code.
10%
Flag icon
In Swift, parentheses around conditional statements are optional;
10%
Flag icon
For style purposes, it is recommended that you do not include parentheses in your code unless you have multiple conditional statements on the same line.
10%
Flag icon
There is one other parameter that we can add to our print() function: the to: parameter. This parameter will let us redirect the output of the print() function. In the following example, we redirect the output to a variable named line: var name1 = "Jon" var name2 = "Kim" var name3 = "Kailey" var name4 = "Kara"   var line = ""   print(name1, name2, name3, name4, separator:", ", terminator:"", to:&line) print(line)
12%
Flag icon
Unless there is a specific reason to define the size of an integer, I would recommend using the standard Int or UInt types.
13%
Flag icon
let speedOfLightKmSec = 300_000
13%
Flag icon
The Swift compiler will ignore these underscores and interpret this value as if the underscores were not there.
13%
Flag icon
One of the new features of Swift 5 is the isMultiple(of:) method, which was added to the integer type. This method...
This highlight has been truncated due to consecutive passage length restrictions.
13%
Flag icon
It is recommended that we use the Double type over the Float type unless there is a specific reason to use the latter.
13%
Flag icon
Starting with Swift 4.2, we can use the toggle() method like this: isItRaining.toggle()
Christopher Grey
Booleans can be toggled with a method.
14%
Flag icon
let str1 = #"The main character said "hello""# Notice the hashtag and double quotes at the start and end of the string. That tells Swift that this is a raw string. This makes it much easier to read what the string actually contains. If we want to append another string in-line, as we did previously, we would use the \#() character sequence. The following code illustrates this: let ans = 42 var str2 = #"The answer is \#(ans)"# The result of this code would be a str2 variable containing the following string: The answer is 42.
15%
Flag icon
The values of the tuple can be decomposed into a set of variables, as shown in the following example: var team = ("Boston", "Red Sox", 97, 65, 59.9)   var (city, name, wins, losses, percent) = team
15%
Flag icon
Naming tuples, known as named tuples, allows us to avoid the decomposition step. A named tuple associates a name (key) with each element of the tuple. The following example shows how to create a named tuple: var team = (city:"Boston", name:"Red Sox", wins:97, losses:65, percent:59.9)
15%
Flag icon
Values from a named tuple can be accessed using the dot syntax.
15%
Flag icon
Enumerations (also known as enums) are a special data type that enables us to group related types together and use them in a type-safe manner.
16%
Flag icon
If integers are used for the raw values of an enumeration, then we do not have to assign a value to each member. If no value is present, the raw values will be auto-incremented.
21%
Flag icon
Swift provides special type aliases for working with nonspecific types. These aliases are AnyObject and Any. We can use these aliases to define arrays whose elements are of different types, like this: var myArray: [Any] = [1,"Two"]
21%
Flag icon
The following examples show the two ways to create a two-dimensional array in Swift: var multiArrayOne = [[1,2],[3,4],[5,6]]   var multiArrayTwo = [[Int]]()
21%
Flag icon
The following example shows how we can define a two-dimensional array and retrieve an individual value within the two dimensions: let multiArray = [[1,2],[3,4],[5,6]] let arr = multiArray[0]         //arr contains the array [1,2]   let value = multiArray[0][1]    //value contains 2