发布值上的SwiftUI toggle()函数停止使用Swift 5.2触发didSet [英] SwiftUI toggle() function on Published values stopped triggering didSet with Swift 5.2
问题描述
我刚刚将Xcode从11.3更新为11.4,用SwiftUI编写的项目的行为开始有所不同.我曾经为布尔值调用 toggle()
函数,它曾经触发过 didSet
属性观察器,但是,它不再起作用了.
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.
假设我们有一个名为 isSettingOn
的 State
属性.我曾经这样称呼过:
Let' say we have a State
property called isSettingOn
. I used to call this:
isSettingOn.toggle()
正在触发该属性的 didSet
观察者.现在,只有当我打电话给我时:
which was triggering didSet
observer of the property. Now, only if I call this:
isSettingOn = true
正在工作.
我的项目都基于这种行为,现在这种变化基本上破坏了一切.有人知道我在这里实际上是否做错了什么?
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?
添加了演示代码:
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")
}
}
}
推荐答案
这是 Xcode 11.4和11.4.1
中的错误,并已在 Xcode 11.5(Beta)中修复代码>带有
Swift 5.2.4
.
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
.
这篇关于发布值上的SwiftUI toggle()函数停止使用Swift 5.2触发didSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!