1

我有两个字符串发布者和一个返回 AnyPublisher 的计算属性。逻辑很简单,但我想知道是否有任何方法可以传播初始值。我认为这应该是可能的,因为出版商有初始值。

在 VC 中,我从 ViewModel(来自 textField)为 Publishers 分配新值。

firstTextField.addTarget(self, action: #selector(firstTextFieldDidChange(_:)), for: .editingChanged)
secondTextField.addTarget(self, action: #selector(secondTextFieldDidChange(_:)), for: .editingChanged)

@objc private func firstTextFieldDidChange(_ textField: UITextField) {
 viewModel.firstPublisher = textField.text ?? ""
}
@objc private func secondTextFieldDidChange(_ textField: UITextField) {
 viewModel.secondPublisher = textField.text ?? ""
}

然后我将 Publisher (combineLatest) 分配给我的按钮:

_ = viewModel.validatedText
   .receive(on: RunLoop.main)
   .assign(to: \.isEnabled, on: button)

在 VM 中,我有两个发布者:

@Published var firstPublisher: String = ""
@Published var secondPublisher: String = ""

和CombineLatest:

var validatedText: AnyPublisher<Bool, Never> {
    return Publishers.CombineLatest($firstPublisher, $secondPublisher) {
        return !($0.isEmpty || $1.isEmpty)
        }.eraseToAnyPublisher()
}

当我开始在两个文本字段中输入时,validatedText 才开始发布新值。例如,我尝试在 VM 的 init 中分配一些新值(给第一个和第二个 Publisher),但它也不起作用。有什么方法可以做到这一点,否则我将不得不在不使用组合的情况下设置按钮的初始状态(禁用它)?

4

1 回答 1

4

不幸的是,这似乎只是 的行为@Published,但您可以在生成的 Publisher 中通过添加初始值来解决此问题:

var validatedText: AnyPublisher<Bool, Never> {
    let validate: (String, String) -> Bool = {
        !($0.isEmpty || $1.isEmpty)
    }
    return Publishers.CombineLatest($firstPublisher, $secondPublisher, transform: validate)
        .prepend(validate(firstPublisher, secondPublisher))
        .eraseToAnyPublisher()
}

相反,如果您宁愿采用这种方法,编写自己的属性委托来获得您想要的行为是相当简单的:

import Combine

@propertyDelegate
struct InitialPublished<Value> : Publisher {
    typealias Output = Value
    typealias Failure = Never

    private let subject: CurrentValueSubject<Output, Failure>

    var value: Value {
        set { subject.value = newValue }
        get { subject.value }
    }

    init(initialValue: Value) {
        subject = CurrentValueSubject(initialValue)
    }

    func receive<S>(subscriber: S) where S: Subscriber, Value == S.Input, Failure == S.Failure {
        subject.receive(subscriber: subscriber)
    }
}
于 2019-06-30T15:54:37.303 回答