More on this book
Kindle Notes & Highlights
if we want to use the comments to also document our code, we need to use the triple slash (///) or multiline comment block.
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/
For style purposes, it is strongly recommended that you do not use semicolons in your Swift code.
In Swift, parentheses around conditional statements are optional;
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.
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)
Unless there is a specific reason to define the size of an integer, I would recommend using the standard Int or UInt types.
let speedOfLightKmSec = 300_000
The Swift compiler will ignore these underscores and interpret this value as if the underscores were not there.
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.
It is recommended that we use the Double type over the Float type unless there is a specific reason to use the latter.
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.
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
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)
Values from a named tuple can be accessed using the dot syntax.
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.
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.
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"]
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]]()
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

