Does not conform to protocol BindableObject - Xcode 11 Beta 4

前端 未结 3 1088
暗喜
暗喜 2021-02-07 03:43

Playing around with examples out there. Found a project that had a class that was a bindableobject and it didn\'t give any errors. Now that Xcode 11 beta 4 is out

3条回答
  •  广开言路
    2021-02-07 04:14

    Beta 4 Release notes say:

    The BindableObject protocol’s requirement is now willChange instead of didChange, and should now be sent before the object changes rather than after it changes. This change allows for improved coalescing of change notifications. (51580731)

    You need to change your code to:

    class UserSettings: BindableObject {
    
        let willChange = PassthroughSubject()
    
        var score: Int = 0 {
            willSet {
                willChange.send()
            }
        }
    }
    

    In Beta 5 they change it again. This time they deprecated BindableObject all together!

    BindableObject is replaced by the ObservableObject protocol from the Combine framework. (50800624)

    You can manually conform to ObservableObject by defining an objectWillChange publisher that emits before the object changes. However, by default, ObservableObject automatically synthesizes objectWillChange and emits before any @Published properties change.

    @ObjectBinding is replaced by @ObservedObject.

    class UserSettings: ObservableObject {
        @Published var score: Int = 0
    }
    
    struct MyView: View {
        @ObservedObject var settings: UserSettings
    }
    

提交回复
热议问题