问题描述
玩弄那里的例子.发现一个项目的类是 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"
它有一个关于错误的修复按钮,当你点击它时,它会添加
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)
您需要将代码更改为:
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)
您可以通过定义一个手动符合 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!