Swift Structs

A struct groups stored properties and methods into a value type. Swift generates a memberwise initializer, so Point(x: 3, y: 4) works without writing one.

Swift
struct Point {
    var x: Int
    var y: Int

    func distanceSquared() -> Int {
        return x * x + y * y
    }
}

var point = Point(x: 3, y: 4)
print(point.distanceSquared())
point.x = 6
print(point.distanceSquared())
Read the full lesson