C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: The property may be accessed from the calling code. It can be read or assigned.
Var: The property "color" is declared with var because it is variable. A constant property uses let.
Var, LetSwift program that uses property, class
class Square {
var color: String
init(color: String) {
// Initialize the property.
self.color = color
}
}
// Create instance of class.
var test = Square(color: "blue")
// Print property value.
print(test.color)
// Reassign property and print it again.
test.color = "red"
print(test.color)
Output
blue
red
Instead: The lazy property is initialized right before it is first accessed. Further accesses then reuse the data.
Note: A lazy property calls an init method like Color() here—it cannot use just a value.
Here: We see that "color" is initialized right before its first access. Without lazy, Color() is run earlier.
Swift program that uses lazy property
class Color {
var name: String
init() {
print("Color init called")
self.name = "Blue"
}
}
class Square {
lazy var color: Color = Color()
init() {
print("Square init called")
}
}
var test = Square()
print("Before property use")
// Use property.
// ... It is initialized before first use.
print(test.color.name)
print(test.color.name)
print("After property use")
Output
Square init called
Before property use
Color init called
Blue
Blue
After property use
Output, without lazy
Color init called
Square init called
Before property use
Blue
Blue
After property use
Get: This is run when the property is accessed. Here it returns the wingLength times 2.
Set: This stores a value (with the keyword "newValue"). This writes to the class memory.
NewValue: This is a special name in a set computed property. It refers to the value the property is being set to.
Swift program that uses computed properties, get and set
class Bird {
var wingLength: Int
var wingSpan: Int {
get {
// This computed property is based on wingLength.
return wingLength * 2
}
set {
// Store the results of a computation.
wingLength = newValue / 2
}
}
init() {
self.wingLength = 0
}
}
var parrot = Bird()
// We write and read the results of the computed properties.
parrot.wingSpan = 2
print(parrot.wingSpan)
Output
2
Note: The "get" keyword can be omitted when no "set" is present. The get is assumed by the Swift compiler.
Swift program that uses read-only property
class Car {
var damageLevel: Int
var isJunker: Bool {
// This is a computed read-only property.
return damageLevel >= 10
}
init(damageLevel: Int) {
self.damageLevel = damageLevel
}
}
// This car has a high level of damage.
let car1 = Car(damageLevel: 20)
print(car1.isJunker)
// This car has a low level of damage.
let car2 = Car(damageLevel: 1)
print(car2.isJunker)
Output
true
false
WillSet: This is triggered by an assignment to the property. It allows us to read the current value before it is changed.
DidSet: This lets us read both values—the previous one and the new one that was just set.
OldValue: A special name in the didSet method. It is the previous value of the property before it was altered.
Swift program that uses property observers, willSet, didSet
class Volume {
var level: Int = 0 {
willSet {
// Print current value.
print("willSet")
print(level)
}
didSet {
// Print both oldValue and present value.
print("didSet")
print(oldValue)
print(level)
}
}
}
// Use property on class.
// ... Trigger willSet and didSet observers.
var v = Volume()
v.level = 4
Output
willSet
0
didSet
0
4