简体   繁体   English

已发布值上的 SwiftUI toggle() 函数停止使用 Swift 5.2 触发 didSet

[英]SwiftUI toggle() function on Published values stopped triggering didSet with Swift 5.2

I have just updated my Xcode to 11.4 from 11.3 and my project written in SwiftUI started to behave differently.我刚刚将我的 Xcode 从 11.3 更新到 11.4,我用 SwiftUI 编写的项目开始出现不同的行为。 I used to call toggle() function for boolean values and it used to trigger didSet property observer, however, it is not working any more.我曾经为布尔值调用toggle()函数,它用于触发didSet属性观察器,但是,它不再起作用了。

Let' say we have a State property called isSettingOn .假设我们有一个名为isSettingOnState属性。 I used to call this:我曾经这样称呼:

isSettingOn.toggle()

which was triggering didSet observer of the property.这是触发属性的didSet观察者。 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")
        }
    }
}

这是Xcode 11.4 and 11.4.1中的一个错误,它在Xcode 11.5 (Beta)Swift 5.2.4得到修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift @Published 导致 didSet 递归 - Swift @Published causing didSet recursion 在不触发 didSet {} 的情况下更改 @Published object 值? - Change the @Published object value without triggering the didSet {}? SwiftUI:动作不会通过切换触发 - SwiftUI: Action not triggering with toggle SwiftUI - 更改@Published 结构时是否可以触发 didSet? - SwiftUI - is it possible to get didSet to fire when changing a @Published struct? DidSet在初始化功能中不起作用Swift 3 - DidSet not working in init function swift 3 如何正确格式化 swift @Published didSet 中附加到 TextField 的字符串? - how to properly format string in swift @Published didSet attached to the TextField? 在 didSet 之后观察 Swift 中 @Published var 的变化? - Observe change on a @Published var in Swift Combine after didSet? @Published Realm object SwiftUI 不触发视图重绘 - @Published Realm object not triggering view redrawing in SwiftUI 当我通过鼠标单击更改 SwiftUI 列表的选择时,@Published 属性的 didSet 被调用了两次 - When I change selection of the SwiftUI List with mouse click didSet of the @Published property is called twice 通过 @Published var 上的 didSet 将日期/时间保存到 UserDefaults(来自 SwiftUI 的 TimePicker 组件) - Saving Date/Time to UserDefaults through didSet on @Published var (from TimePicker component of SwiftUI)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM