Introduction
Properties and methods are how structs, classes, and enums expose data and behavior. Properties come in two flavors: stored properties that hold an actual value in memory, and computed properties that calculate a value on demand via a getter and optionally a setter. Methods are functions associated with a particular type; instance methods operate on a specific instance, while type methods, declared with static func, operate on the type itself.
Cricket analogy: A batsman's career strike rate is stored after each match like a stored property, while his current-series average is a computed property recalculated from stored innings data every time you check it; team methods like declareInnings() act on the whole XI, like a static function.
Syntax
struct Rectangle {
var width: Double
var height: Double
var area: Double { // computed property
return width * height
}
mutating func scale(by factor: Double) {
width *= factor
height *= factor
}
static func unitSquare() -> Rectangle {
Rectangle(width: 1, height: 1)
}
}Explanation
width and height are stored properties, holding actual values. area is a computed property — it has no storage of its own and instead recalculates its value from width and height every time it's accessed. scale(by:) is an instance method that mutates the struct's own properties; because structs are value types, any instance method that changes self must be marked mutating. unitSquare() is a type method, callable on the type itself (Rectangle.unitSquare()) rather than on an instance. Property observers willSet and didSet can be attached to stored properties to run code just before or after a value changes.
Cricket analogy: A player's stored runs total sits in the scorebook like width and height, but strike rate is a computed property recalculated from runs and balls faced every time it's displayed; changing your batting stance mid-innings is like a mutating method, and Team.selectSquad() is a type method run once before the match.
Example
struct Rectangle {
var width: Double { didSet { print("width changed to \(width)") } }
var height: Double
var area: Double { width * height }
mutating func scale(by factor: Double) {
width *= factor
height *= factor
}
}
var r = Rectangle(width: 3, height: 4)
print("area = \(r.area)")
r.scale(by: 2)
print("area = \(r.area)")Output
area = 12.0
width changed to 6.0
area = 24.0Key Takeaways
- Stored properties hold actual values; computed properties calculate a value on access.
- Property observers willSet and didSet run code around a stored property's value changing.
- Instance methods operate on a specific instance; type methods use static func and operate on the type.
- Struct instance methods that mutate self must be marked mutating.
- Computed properties can have a getter only, or both a getter and a setter.
Practice what you learned
1. What is a computed property?
2. Which keyword must a struct's instance method use if it modifies the struct's own properties?
3. What does static func declare?
4. Which property observer runs just after a stored property's value has changed?
5. Does a class instance method that mutates a property need the mutating keyword?
Was this page helpful?
You May Also Like
Structs in Swift
Structs are value types in Swift that are copied on assignment and come with a free memberwise initializer.
Classes in Swift
Classes are reference types in Swift that support inheritance and are managed automatically via ARC.
Structs vs Classes in Swift
Structs use value semantics while classes use reference semantics — the defining choice you make for every custom Swift type.
Functions in Swift
Learn how to declare, call, and label the parameters of reusable blocks of code called functions in Swift.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics