swift learning

Optional

  1. The question mark (?) at the end denotes that it is an optional variable.
  2. 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.

  3. 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 if age is Int, ageOfHumanInHouse still is an optional type.
  4. 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.”
  5. weak mark a variable which should be optional type to solve the retain cycles.

Closure

  1. Closure is a reference type
  2. Using captured list [] before the in block to make own copy of arguments instead of Referencing.

Function

  1. 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

  1. 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, self can be used after super.init
  2. 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)
  3. Failable Initializers

    • using init? creates an optional instance(can be nil, type is optional)
    • call from subclass using super.init without ?

Property

  1. Stored Properties

  2. Computed Properties

    • with getter and setter
  3. 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 willSet and didSet
  4. Lazy Properties

    • A lazy stored 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.
  5. Type Properties

    • static belongs to type itself