Swift 的 mutating
关键字修饰方法是为了能在该方法中修改 struct
或是 enum
的变量
protocol Person {
mutating func changeName()
}
struct User: Person {
var name = ""
mutating func changeName() {
name = "lisi"
}
}
var user = User(name: "zhangsan")
//zhangsan
user.name
user.changeName()
//lisi
user.name
网友评论