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

筅森魡賤 提交于 2021-01-29 19:08:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!