0Pricing
Swift Academy · Lesson

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

  1. Memberwise init & mutating methods
  2. Inheritance, overriding & final
  3. Access control (public/internal/fileprivate/private)
← Back to Swift Academy