Properties
In Swift, properties associate values with a particular class, structure, or enumeration. There are two kinds of properties:
Stored Properties: These store constant and variable values as part of an instance. Stored properties can be either variable stored properties (introduced by the var keyword) or constant stored properties (introduced by the letkeyword).
Computed Properties: Instead of storing a value, a computed property provides a getter and an optional setter to retrieve and set other properties and values indirectly.
Stored Properties
Stored properties can be either variable (var) or constant (let). They can have default values and can also be set and accessed using dot syntax.
struct Point {
var x: Double
let y: Double
}
var myPoint = Point(x: 1.0, y: 2.0)
myPoint.x = 2.0 // OK
// myPoint.y = 3.0 // Error, `y` is a constant property
Lazy Stored Properties
A lazy stored property is a property whose initial value is not calculated until the first time it is used.
class DataLoader {
// This is a heavy computational property, for example, loading data from disk
lazy var allData = ["Data1", "Data2", "Data3"]
}
let loader = DataLoader()
print(loader.allData) // Output: ["Data1", "Data2", "Data3"]
Computed Properties
Computed properties provide a getter, and optionally a setter, to access or modify other properties.
struct Circle {
var radius: Double
var diameter: Double {
get {
return radius * 2
}
set {
radius = newValue / 2
}
}
}
var circle = Circle(radius: 5)
print(circle.diameter) // Output: 10.0
circle.diameter = 12
print(circle.radius) // Output: 6.0
Property Observers
Swift allows you to define property observers to monitor changes in a property’s value, which you can respond to with custom actions. Observers can be added to stored properties you define yourself and properties that a subclass inherits.
- willSet is called just before the value is stored.
- didSet is called immediately after the new value is stored.
struct Rectangle {
var width: Double {
willSet {
print("About to set width to \(newValue)")
}
didSet {
print("Width changed from \(oldValue) to \(width)")
}
}
}
var rect = Rectangle(width: 10)
rect.width = 20
print(rect) // Rectangle with = 20.0
Type Properties
You can also define properties that belong to the type itself, rather than to any one instance of that type.
struct SomeStructure {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 27
}
}
enum SomeEnumeration {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 6
}
}
class SomeClass {
static var storedTypeProperty = "Some value."
class var computedTypeProperty: Int {
return 42
}
}
Readonly Computed Properties
A computed property with a getter but no setter is known as a read-only computed property. You can simplify the declaration of a read-only computed property by removing the get keyword and its braces.
struct Cuboid {
var width: Double
var height: Double
var depth: Double
var volume: Double {
return width * height * depth
}
}
let vol = Cuboid(width: 2, height: 4, depth: 6)
print(vol.volume)
In Swift, properties play an important role in encapsulation, computation, and observation, serving as a fundamental building block of your data model. They provide a unified interface for data manipulation, improving readability, maintainability, and reusability of your code.