问题
I have just updated my Xcode to 11.4 from 11.3 and my project written in SwiftUI started to behave differently. I used to call toggle()
function for boolean values and it used to trigger didSet
property observer, however, it is not working any more.
Let' say we have a State
property called isSettingOn
. I used to call this:
isSettingOn.toggle()
which was triggering didSet
observer of the property. Now, only if I call this:
isSettingOn = true
it is working.
My projects are all based on this behaviour and now this change basically broke everything. Does anyone know if I am actually doing anything wrong here?
Edit:
Demo code added:
struct ContentView: View {
@State var isSettingOn: Bool = true {
didSet {
print("didSet isSettingOn")
}
}
var body: some View {
Button(action: {
self.isSettingOn = true // will trigger didSet
self.isSettingOn.toggle() // won't trigger didSet
}) {
Text("Toggle isSettingOn")
}
}
}
回答1:
This was a bug in Xcode 11.4 and 11.4.1
and it was fixed in Xcode 11.5 (Beta)
with Swift 5.2.4
.
来源:https://stackoverflow.com/questions/60907882/swiftui-toggle-function-on-published-values-stopped-triggering-didset-with-swi