Swift Flashcards
How to label loops?
sum = 0 rowLoop: for row in 0..<8 { columnLoop: for column in 0..<8 { if row == column { continue rowLoop } sum += row * column } }
what is overloading?
When functions have the same name with different parameters.
What is special Never return type in func?
wift that this function will never exit.
func infiniteLoop() -> Never { while true { } }
Writing good functions
The best do /one simple task/, making them easier to mix, match, and model into more complex behaviors.
nil coalescing
??
make an ArraySlice
Use countable ranges
let upcomingPlayersSlice = players[1...2] print(upcomingPlayersSlice[1], upcomingPlayersSlice[2]) // > "Bob Cindy\n"
If you just want to know whether a dictionary has elements or not
is always better to use the isEmpty property. A dictionary needs to loop through all of the values to compute the count. isEmpty, by contrast, always runs in constant time no matter how many values there are
If you want keep the key and set the value to nil
you must use the updateValue method.
dictionary[key] = nil removes the key completely
Rule the hash value
always return the same hash value
When use sets?
If you want to ensure that an item doesn’t appear more than once in your collection, and when the order of your items isn’t important.
Swift Dictionaries
- Are unordered collections of key-value pairs.
The keys are all of the same type, and the values*are all of the same type.
Use subscripting* to get values and to add, update or remove pairs.
- If a key is not in a dictionary, lookup returns nil.
The key of a dictionary must be a type that conforms to the Hashable*protocol.
- Basic Swift types such as String, Int, Double are Hashable out of the box.
Swift Arrays
- Are ordered collections of values of the same type.
Use subscripting*, or one of the many properties and methods, to access and update elements.
- Be wary of accessing an index that’s out of bounds.
Swift closure
Simply a function with no name; you can assign it to a variable and pass it around like any other value.
Closures are so named because they have the ability to “close over” the variables and constants within the closure’s own scope. This simply means that a closure can access, store and manipulate the value of any variable or constant from the surrounding context. Variables and constants used within the body of a closure are said to have been capturedby the closure.
A closure can capture*the variables and constants from its surrounding context.
- A closure can be used to direct how a collection is sorted.
trailing closure syntax
you declare body after other parameters as its body of func, but its body of closure that are inside.
compactMap vs map
This is almost the same as map except it creates an array and tosses out the missing values
Swift character
A Character is grapheme cluster and is made up of one or more code points.
You use special (non-integer) indexes to subscript into the string to a certain grapheme cluster.
rapheme cluster
A Character is grapheme clusterand is made up of one or more code points.
What is character in swift
A Character is grapheme clusterand is made up of one or more code points.
What slicing a string produce?
Slicing a string produce a substring with type Substring, which shares storage with its parent String.
You can convert from a Substring to a String by initializing a new String and passing the Substring.
Structures
They are value types, which means their values are copied on assignment.
Structures are also very /fast/compared to their reference alternatives,
What is faster? Structures or classes? Why?
Structures. Structs rely on the faster stack while classes rely on the slower heap
Stored properties vs computed properties. What have each of them?
Stored properties allocate memory to store a value.
Computed properties are calculated each time your code requests them and aren’t stored as a value in memory.
Do not confuse property observers with getters and setters. A stored property can have a didSet and/or a willSet observer. A computed property has a getter and optionally a setter
read-only computed property vs read-write computed property
read-write computed property: a getter and a setter.
read-only: only getter
lazy modifier
The lazy modifier prevents a value of a stored property from being calculated until your code uses it for the first time. You’ll want to use lazy initializationwhen a property’s initial value is computationally intensive or when you won’t know the initial value of a property until after you’ve initialized the object.