简体   繁体   中英

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. 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")
        }
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM