Tsallaka zuwa abun ciki
Justice Tech School

Methods and mutating

Methods on structs that change the instance must be marked mutating.

Example
struct Counter {
    var n = 0
    mutating func inc() { n += 1 }
}
var c = Counter()
c.inc(); c.inc()
print(c.n)

Exercise

Change self.

 func inc()

Quiz Test

Why mutating?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.