
Optional
- The question mark (?) at the end denotes that it is an optional variable.
-
Unwrapping optionals
- Force unwrapping: The exclamation point (!) at the end of the variable is used to unwrap the value.
- Implicit unwrapping: These types of optionals are declared with an (!) instead of a (?), for example:
let someString: String! -
Optional Binding:
- if-let/var statement
Constants and variables created with optional binding in an if statement are available only within the body of the if statement.
- Guard Statement
the constants and variables created with a guard statement are available in the lines of code that follow the guard statement.
- if-let/var statement
-
options inside class
- Optional chaining: using an optional with a question mark at the end, anytime you are going to use the dot(.) to access the property or something, such as
let ageOfHumanInHouse = houseObj.human?.age, even ifageisInt,ageOfHumanInHousestill is an optional type.
- Optional chaining: using an optional with a question mark at the end, anytime you are going to use the dot(.) to access the property or something, such as
- The nil-coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil. The expression a is always of an optional type. The expression b must match the type that is stored inside a.”
weakmark a variable which should be optional type to solve the retain cycles.
Closure
- Closure is a reference type
- Using captured list [] before the
inblock to make own copy of arguments instead of Referencing.
Function
- Function parameters are constants by default. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead. such as
func swapTwoInts(_ a: inout Int, _ b: inout Int)
Initialization
-
Two-Phase Initialization
- 1st: each stored property is assigned an initial value by the class that introduced it, subclass need init its own value first, then call
super.init - 2nd: class is given the opportunity to customize its stored properties,
selfcan be used aftersuper.init
- 1st: each stored property is assigned an initial value by the class that introduced it, subclass need init its own value first, then call
-
designated vs Convenience initializers
- A designated initializer must call a designated initializer from its immediate superclass.
- A convenience initializer must call another initializer from the same class.
- A convenience initializer must ultimately call a designated initializer. (call a chain of another Convenience Initializer, but at last should call the Designated Initializer)
-
Failable Initializers
- using
init?creates an optional instance(can be nil, type is optional) - call from subclass using
super.initwithout?
- using
Property
-
Stored Properties
-
Computed Properties
- with
getterandsetter
- with
-
Property Observers
- can add property observers to any stored properties you define, except for lazy stored properties
- can add property observers to any inherited property (whether stored or computed) by overriding the property within a subclass.
- don’t need to define property observers for nonoverridden computed properties, because you can observe and respond to changes to their value in the computed property’s setter.
- with
willSetanddidSet
-
Lazy Properties
- A
lazystored property is a property whose initial value is not calculated until the first time it is used - using closure or function to initialize the lazy property.
- A
-
Type Properties
staticbelongs to type itself




近期评论