我正在尝试动态更新对@Published var
的引用,但我不知道该怎么做。只返回值只是一个布尔值,它会丢失对发布者的引用,无法工作。我尝试返回发布者本身($self.isBig
(,但我似乎不知道如何在拥有发布者而不是@Published属性后更新值。
我基本上只是尝试将@Published
作为引用,并更新类上的引用,而不是复制发布者的值。
这是一个人为的例子,只是想让人们明白这一点,我想做的是:
import UIKit
import Combine
class MyClass {
struct MyData {
enum MyDataType {
case big
case yellow
case bird
}
var type: MyDataType
var isEnabled: Bool
}
private var cancellables = Set<AnyCancellable>()
@Published var isBig: Bool = false
@Published var isYellow: Bool = false
@Published var isBird: Bool = false
var mySwitch = UISwitch()
init() {
// Add mySwitch and setupSubscribers...
}
func updatePublishers(from data: MyData) {
let publisherForData = specificPublisherForData(data)
// I want to access the underlying value for the @Published and set it here
// publisherForData = data.isEnabled
}
func specificPublisherForData(_ data: MyData) -> Published<Bool>.Publisher {
switch data.type {
case .big: return self.$isBig
case .yellow: return self.$isYellow
case .bird: return self.$isBird
}
}
func setupSubscribers() {
$isBig.sink { [weak self] isBig in
guard let self = self else { return }
self.mySwitch.isOn = isBig
}.store(in: &cancellables)
// ... add subscribers for the other ones
}
}
看起来您正试图为updatePublishers
中的Publisher
分配一个新值,但@Publisher
并不是这样工作的——它们只是在广播值。
相反,使用密钥路径似乎是你想要的:
class MyClass {
struct MyData {
enum MyDataType {
case big
case yellow
case bird
}
var type: MyDataType
var isEnabled: Bool
}
private var cancellables = Set<AnyCancellable>()
@Published var isBig: Bool = false
@Published var isYellow: Bool = false
@Published var isBird: Bool = false
var mySwitch = UISwitch()
init() {
// Add mySwitch and setupSubscribers...
}
func updatePublishers(from data: MyData) {
let keypath = specificKeypathForData(data)
self[keyPath: keypath] = data.isEnabled
}
func specificKeypathForData(_ data: MyData) -> ReferenceWritableKeyPath<MyClass,Bool> {
switch data.type {
case .big: return .isBig
case .yellow: return .isYellow
case .bird: return .isBird
}
}
func setupSubscribers() {
$isBig.sink { [weak self] isBig in
guard let self = self else { return }
self.mySwitch.isOn = isBig
}.store(in: &cancellables)
// ... add subscribers for the other ones
}
}
在操场上:
var myClass = MyClass()
myClass.setupSubscribers()
var data = MyClass.MyData(type: .big, isEnabled: true)
myClass.updatePublishers(from: data)
print(myClass.isBig)
收益率:
真实