gpt4 book ai didi

ios - swift中赋值时如何避免set方法?

转载 作者:行者123 更新时间:2023-11-30 13:44:57 25 4
gpt4 key购买 nike

在objective-c中我们可以使用self.foo = xxx来调用-setFoo:方法并使用_foo = xxx来避免它。

但是我们在使用 Swift 时如何避免 set 呢?

我就是这样做的。

// use foo = xxx to call set
public var foo: Int {
set {
fooInside = newValue
// ...
bar()
}
get { return fooInside }
}
// use fooInside = xxx to avoid set
private var fooInside = 0

我们有更优雅的方式吗?谢谢。

最佳答案

我认为扩展这样做很优雅:

extension Int {
mutating func setValue(value: Int,update: Bool) {
self = value
if update {

}
}
}

关于ios - swift中赋值时如何避免set方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35078580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com