Properties are values associated with a particular class or structure. Unlike other languages, Swift encourages you to use and access properties directly.
These properties are stored directly in the class.
struct FixedLengthRange {
var firstValue: Int
let length: Int
}
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 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.
Usefull for when you have complicated setup.
var foo: Int = {
return 1
}()
These are getters with no setters. They must be var
s. 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
}
}
A lazy property is one where the initial value is not calculated until needed.
Note: Lazy properties are always
var
s 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.
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
}
}
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"]