Properties in Swift

An Introduction to Stored and Computed Properties in Swift

Pavlos Simas
2 min readDec 16, 2022

--

Photo by Maxwell Nelson on Unsplash

Properties in Swift are used to store values and associate them with a particular instance of a type. There are two main types of properties in Swift: stored properties and computed properties.

Stored properties are variables or constants that are associated with an instance of a type and stored as part of that instance. They can be either variable or constant, depending on whether they are declared with the var or let keyword. For example:

struct Point {
var x: Double
var y: Double
}

let point = Point(x: 0, y: 0)
point.x = 10
point.y = 20

Computed properties, on the other hand, do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties or variables. The value of a computed property is calculated every time it is accessed. For example:

struct Rectangle {
var origin: Point
var size: Size
var center: Point {
get {
Point(x: origin.x + size.width / 2, y: origin.y + size.height / 2)
}
set(newCenter) {
origin.x = newCenter.x - size.width / 2
origin.y = newCenter.y - size.height / 2
}
}
}

var rect = Rectangle(origin: Point(x: 0, y: 0), size: Size(width: 10, height: 10))
print(rect.center) // (5.0, 5.0)
rect.center = Point(x: 15, y: 15)
print(rect.origin) // (10.0, 10.0)

Properties in Swift can also have additional behavior added to them through property observers. Property observers are called every time a property’s value is set, and can be used to perform additional tasks such as logging or validation. There are two property observers in Swift: willSet and didSet. willSet is called just before the value is stored, and didSet is called immediately after. For example:

struct Progress {
var total: Int = 100
var current: Int = 0 {
willSet {
print("About to set current progress to \(newValue)")
}
didSet {
if current > total {
current = total
}
}
}
}

var progress = Progress()
progress.current = 50
// prints "About to set current progress to 50"
progress.current = 110
// current progress is now 110

In addition to these basic property types, Swift also provides a number of other property features that can be useful in certain situations. These include:

  • lazy properties, which are not initialized until they are first accessed
  • static properties, which are associated with the type itself rather than with individual instances
  • final properties, which cannot be overridden by subclasses

Properties are an essential part of working with Swift and are used extensively in iOS development. They provide a convenient way to store and access values associated with a particular instance of a type, and can be customized with additional behavior through property observers and other features.

--

--

Pavlos Simas

iOS Developer, with passion about Development, Marketing and Finance. Join Medium from the following link: https://simaspavlos.medium.com/membership