我正在学习MVVM+RxSwift。
我想保存一个值,以便在下一个流中重用它。
但我遇到了上面的错误。
我假设我初始化了“translatedText”的值,但仍然会出现错误。
我试过一些用声明开头的方法,等等。。。
import UIKit
import RxSwift
final class ViewModel {
private let api: apiProtocol
let validationText: Observable<String>
let getObservable: Observable<String>
var translatedText: String
// var translatedText: String = "" //this case also dosn't work
init(inputObservable: Observable<String?>, changeButtonClicked: Observable<Void>, model: ModelProtocol, api: apiProtocol = APICntl()) {
self.api = api
self.translatedText = "" //I guess I initialised this here
let event = inputObservable
.flatMap { input -> Observable<Event<Void>> in
if let text = input {
self.translatedText = text // error. i want to save "input" to use at "let tapEvent"
}
return model
.validate(text: input)
.materialize()
}
.share()
self.validationText = event
.flatMap { event -> Observable<String> in
switch event {
case .next:
return .just("")
case let .error(error as ModelError):
return .just(error.errorLabel)
case .error, .completed:
return .empty()
}
}
.startWith(ModelError.invalidBlank.errorLabel)
let tapEvent = changeButtonClicked
.flatMap { (result) -> Observable<Event<String>> in
return api
.fetch(text: self.translatedText) // I want use it here
.materialize()
}
.share()
self.getObservable = tapEvent
.flatMap { event -> Observable<String> in
switch event {
case .next:
return .just(event.element!)
case let .error(error as ModelError):
return .just(error.errorLabel)
case .error, .completed:
return .empty()
}
}
}
}
你们有更好的解决办法吗?
-----自解-----
我只是在let事件之前创建了一个临时变量,它可以按我的要求工作。
final class ViewModel {
private let api: apiProtocol
let validationText: Observable<String>
let getObservable: Observable<String>
init(inputObservable: Observable<String?>, changeButtonClicked: Observable<Void>, model: ModelProtocol, api: apiProtocol = APICntl()) {
self.api = api
var temp = ""
let event = inputObservable
.flatMap { input -> Observable<Event<Void>> in
if let text = input {
temp = text
}
return model
.validate(text: input)
.materialize()
}
.share()
最佳答案
你不应该用一些随机的“临时”内存块把两个独立的可观察流相互联系起来。这两个流依赖于inputObservable
,这一点应该清楚。而且,您的视图模型可以大大简化。。。
final class ViewModel {
let validationText: Observable<String>
let getObservable: Observable<String>
init(inputObservable: Observable<String?>, changeButtonClicked: Observable<Void>, model: ModelProtocol, api: apiProtocol = APICntl()) {
let translatedText = inputObservable.share() // this line is unnecessary if `inputObservable` is already hot.
self.validationText = translatedText
.flatMap { input in
return model
.validate(text: input)
.map { "" }
.catchError { error in .just((error as? ModelError)?.errorLabel ?? error.localizedDescription) }
}
.startWith(ModelError.invalidBlank.errorLabel)
self.getObservable = changeButtonClicked
.withLatestFrom(translatedText)
.compactMap { $0 }
.flatMap { translatedText in
return api
.fetch(text: translatedText)
.catchError { error in .just((error as? ModelError)?.errorLabel ?? error.localizedDescription) }
}
}
}
关于swift - 在初始化所有成员之前,闭包捕获了Swift5(+ RxSwift)'self',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56039725/