我的專案中有一個列舉:
enum Remote<Content> {
case .notAsked
case .loading
case .loaded(Content)
case .failed(Error)
}
然后我有一個類:
class MyViewModel: ObservableObject {
@Published var content: Remote<ContentStruct> = .notAsked
func fetchContent() {
content = .loading
service.fetchContent()
.receive(on: queue)
.map(Remote<ContentStruct>.loaded)
.catch { error in Just(.failed(error)) }
.assign(to: &self.content)
}
}
但這向我抱怨:
Cannot convert value of type 'Remote<ContentStruct>' to expected argument type 'Published<Remote<ContentStruct>>.Publisher'
我可以改變它來使用它:
func fetchContent() {
content = .loading
service.fetchContent()
.receive(on: queue)
.map(Remote<ContentStruct>.loaded)
.catch { error in Just(.failed(error)) }
.assign(to: \.content, on: self)
.store(in: &cancellables)
}
這有效并正確分配了值。但我不明白為什么我不能使用.assign(to: keyPath)那里的功能?
我需要做一些不同的事情嗎?我們最近才更新以支持最低限度的 iOS14,因此assign(to:由于記憶體泄漏之前沒有使用過,現在我只是不確定它是如何作業的。
謝謝
uj5u.com熱心網友回復:
assign(to:) requires a Published.Publisher as its input. So you need to pass the publisher from the Publisher property wrapper, which you can access using the $ prefix.
.assign(to: &$content)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491670.html
