从闭包内部改变结构的 属性
Mutating a property of struct from inside a closure
我正在尝试在结构中的变异函数内编写闭包,并从闭包内部更改一个 属性 结构。但它给我的错误如下:
"Escaping closure captures mutating 'self' parameter "
struct Sample {
var a = "Jonh Doe"
mutating func sample() {
let closure = { () in
self.a = "eod hnoj"
}
print(closure)
print(a)
}
}
var b = Sample()
b.sample()
尝试下面的示例代码。
struct Sample {
var a = "Jonh Doe"
mutating func sample() {
let closure = {
Sample(a: "eod hnoj")
}
self = closure()
}
}
var b = Sample()
b.sample()
print(b)
我正在尝试在结构中的变异函数内编写闭包,并从闭包内部更改一个 属性 结构。但它给我的错误如下:
"Escaping closure captures mutating 'self' parameter "
struct Sample {
var a = "Jonh Doe"
mutating func sample() {
let closure = { () in
self.a = "eod hnoj"
}
print(closure)
print(a)
}
}
var b = Sample()
b.sample()
尝试下面的示例代码。
struct Sample {
var a = "Jonh Doe"
mutating func sample() {
let closure = {
Sample(a: "eod hnoj")
}
self = closure()
}
}
var b = Sample()
b.sample()
print(b)