HR's Blog

Swimming 🏊 in the sea🌊of code!

0%

Mutating

.

因为Swift的协议protocol可以被classstructenum来实现,对于structenum如果协议定义的方法需要修改本身的参数,则需要加上mutating,但是class就没有这种限制。

1
2
3
4
5
6
7
8
9
10
11
12
13
protocol Vehicle {
var numberOfWheels: Int {get}
var color: UIColor {get set}
mutating func changeColor()
}

struct MyCar: Vehicle {
let numberOfWheels = 4
var color = UIColor.blueColor()
mutating func changeColor() {
color = UIColor.redColor()
}
}