Memberwise init & mutating methods
Understand Swift structs: auto-generated memberwise initializers and the need for mutating methods when changing properties.
Intro
Structs in Swift get a free memberwise initializer and require mutating for methods that modify state.
Memberwise init
Swift automatically provides an init(x:y:) for structs with stored properties.
struct Point {
var x: Int
var y: Int
}
let p = Point(x: 3, y: 5)
print(p.x, p.y)All lessons in this course
- Memberwise init & mutating methods
- Inheritance, overriding & final
- Access control (public/internal/fileprivate/private)