swiftui - 不符合协议(protocol) BindableObject - Xcode 11 Beta 4

标签 swiftui xcode11

玩弄那里的例子。发现一个项目的类是 bindableobject它没有给出任何错误。现在 Xcode 11 beta 4 已经发布,我得到了错误:

Type 'UserSettings' does not conform to protocol 'BindableObject'



它在错误上有一个修复按钮,当您单击该按钮时,它会添加
typealias PublisherType = <#type#>

它希望您填写类型。

类型是什么?
class UserSettings: BindableObject {

    let didChange = PassthroughSubject<Void, Never>()

    var score: Int = 0 {
        didSet {
            didChange.send()
        }
    }
}

最佳答案

测试版 4 Release notes说:

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)



您需要将代码更改为:

class UserSettings: BindableObject {

    let willChange = PassthroughSubject<Void, Never>()

    var score: Int = 0 {
        willSet {
            willChange.send()
        }
    }
}

Beta 5他们再次改变它。 这次他们一起弃用了 BindableObject!

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
}

关于swiftui - 不符合协议(protocol) BindableObject - Xcode 11 Beta 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57087655/

相关文章:

ios - 更改 bool 值时无法在 SwiftUI 中更改按钮文本

ios - SwiftUI 应用程序可以在预览版中运行,但不能在模拟器中运行

animation - SwiftUI 动画滑入滑出

swift - 编译器错误: Invalid library file - CoreLocation

ios - 单击 tableView 后隐藏 xib 的自定义 UITableViewCell

border - SwiftUI:如何编辑 FocusRing 形状或禁用它?

swift - 特定功能同时在所有 View 上运行。我怎样才能让它一个接一个地运行?

ios - 如何在 Xcode 10 中打开 Xcode 11 Beta 4 项目?

swift - xcode 11 beta 中的这种新导航栏行为是错误还是有意为之?

objective-c - NSKeyValueCoding中的IOS 13和Xcode 11崩溃