gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:39:38 24 4
gpt4 key购买 nike

玩弄那里的例子。发现一个项目的类是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com