不符合 BindableObject 协议 - Xcode 11 Beta 4 [英] Does not conform to protocol BindableObject - Xcode 11 Beta 4

查看:35
本文介绍了不符合 BindableObject 协议 - Xcode 11 Beta 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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, I'm getting the error:

类型UserSettings"不符合协议BindableObject"

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

它有一个关于错误的修复按钮,当你点击它时,它会添加

It has a fix button on the error which when you click on that, it adds

typealias PublisherType = <#type#>

它希望您填写类型.

类型是什么?

class UserSettings: BindableObject {

    let didChange = PassthroughSubject<Void, Never>()

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

推荐答案

Beta 4 Release笔记说:

Beta 4 Release notes say:

BindableObject 协议的要求现在是 willChange 而不是didChange,现在应该在对象改变之前发送比改变之后.此更改允许改进合并更改通知.(51580731)

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!

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

BindableObject 被 ObservableObject 协议取代结合框架.(50800624)

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

您可以通过定义一个手动符合 ObservableObject在对象更改之前发出的 objectWillChange 发布者.但是,默认情况下,ObservableObject 会自动合成objectWillChange 并在任何 @Published 属性更改之前发出.

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 被@ObservedObject 取代.

@ObjectBinding is replaced by @ObservedObject.

class UserSettings: ObservableObject {
    @Published var score: Int = 0
}

struct MyView: View {
    @ObservedObject var settings: UserSettings
}

这篇关于不符合 BindableObject 协议 - Xcode 11 Beta 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆