Skip to content

Latest commit

 

History

History
137 lines (98 loc) · 2.89 KB

Properties.md

File metadata and controls

137 lines (98 loc) · 2.89 KB

Properties

Properties are values associated with a particular class or structure. Unlike other languages, Swift encourages you to use and access properties directly.

Stored Properties

These properties are stored directly in the class.

struct FixedLengthRange {
    var firstValue: Int
    let length: Int
}

Property Observers

Respond to changes in a property's value. Can observe anything except lazy properties.

class StepCounter {
    var totalSteps: Int = 0 {
        willSet(newTotalSteps) {
            print("About to set totalSteps to \(newTotalSteps)")
        }
        didSet {
            if totalSteps > oldValue  {
                print("Added \(totalSteps - oldValue) steps")
            }
        }
    }
}

Computed Properties

Computed properties are computed which means they don't have a backing variable. They don't store any variables directly, but they can change other variables that back them up.

var x:Int

var xTimesTwo:Int {
    set {
       x = newValue / 2
    }
    get {
        return x * 2
    }
}

newValue is the default variable name assigned in the getter. You can assign another if you like.

Computed Property as closure

Usefull for when you have complicated setup.

    var foo: Int = {
        return 1
    }()

Read-Only Computed Properites

These are getters with no setters. They must be vars. You can simplify the declaration of a read-only property by removing the get keyword and braces.

struct Cuboid {
    var width = 0.0, height = 0.0, depth = 0.0
    var volume: Double {
        return width * height * depth
    }
}

Lazy Stored Properties

A lazy property is one where the initial value is not calculated until needed.

Note: Lazy properties are always vars because of their delayed loading.

    lazy var headerView: ActivationHeaderView = {
        return ActivationHeaderView(activationResourcePackage: activationResourcePackage)
    }()

lazy enables class to initialize other resources first, so we can use later in initialization.

set (Computed) or didSet (Stored)?

Use set when the property you are setting is undering a transformation.

var xTimesTwo:Int {
    set {
       x = newValue / 2
    }
}

Use didSet for after the fact processing. Or as swift likes to describe it observing.

var daysPastDue:Int {
    didSet {
       // update label
    }
}

Conditional Initialization

Here is how you can define a variable as a let, but instantiated it's value based on logic.

let entryPoints: [String]

if Chat.shared.hasFailedActivation {
    entryPoints = ["fail"]
}
else {
    entryPoints = ["ios", "myapp"]
}

// or as ternary

let entryPoints = Chat.shared.hasFailedActivation ? ["fail"] : ["ios", "myapp"]

Links that help