[英]Swift call a method after 2 bool didset call
I have 2 bool with a didset method. 我有2个布尔用Didset方法。 Inside both didset it called a same method.
在两个didset内部,它调用了相同的方法。 So I want to call a method after that 2 bool did sets called
所以我想在2个bool设置完后调用一个方法
var canDo: Bool {
didSet {
reload()
}
}
var isView: Bool {
didSet {
reload()
}
}
var canDo: Bool {
didSet {
if isView{
reload()
}
}
}
var isView: Bool {
didSet {
if canDo{
reload()
}
}
}
I'd suggest a new function to handle the logic for when to call reload()
rather than having the logic in both didSet
's. 我建议使用一个新函数来处理何时调用
reload()
的逻辑,而不要在两个didSet
的逻辑中都didSet
该逻辑。 This way if new logic needs to be added it can be added in one place: 这样,如果需要添加新逻辑,则可以将其添加到一个位置:
var canDo: Bool {
didSet { maybeReload() }
}
var isView: Bool {
didSet { maybeReload() }
}
private func maybeReload() {
guard
canDo,
isView
else { return }
reload()
}
We can achieve this with the help of Optionals
我们可以借助
Optionals
Now when update your code like 现在,当像
var canDo: Bool? {
didSet {
if isView != nil {
reload()
}
}
}
var isView: Bool? {
didSet {
if canDo != nil {
reload()
}
}
}
EDIT 编辑
If you meant set as in true
then simply ignore the above and add 如果您的意思是设置为
true
则只需忽略上述内容并添加
var canDo: Bool {
didSet {
if isView {
reload()
}
}
}
var isView: Bool {
didSet {
if canDo {
reload()
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.