gpt4 book ai didi

ios - rxSwift 按钮按下观察器问题

转载 作者:可可西里 更新时间:2023-11-01 00:39:46 25 4
gpt4 key购买 nike

我有一个 init 可以在我的 viewModel 上为 rxSwift 设置观察者的处理

init(login: Observable<String>, password: Observable<String>, buttonPress: Observable<Void>) {
let userInputs = Observable.combineLatest(login, password) { (login, password) -> (String, String) in
return (login, password)
}

let callFromPress = buttonPress
.withLatestFrom(userInputs)
.map { (login, password) in
self.makeLoginRequest(userLogin: login, loginPassword: password, loginSecret: self.loginSecret, deviceToken: self.deviceToken)
}
}

但是当我点击按钮时,没有任何反应。它在 VC 中是这样连接的:

buttonPress: self.loginView.loginButton.rx.tap.asObservable()

关于为什么点击按钮没有触发任何错误的任何解决方案?

更新:

init(login: Observable<String>, password: Observable<String>, buttonPress: Observable<Void>) {
let userInputs = Observable.combineLatest(login, password) { (login, password) -> (String, String) in
return (login, password)
}

buttonPress.subscribe(onNext: { each in
print(each)
.withLatestFrom(userInputs)
.map { (login, password) in
self.makeLoginRequest(userLogin: login, loginPassword: password, loginSecret: self.loginSecret, deviceToken: self.deviceToken)
}
}).disposed(by: self.disposeBag)
}

错误:

Extraneous argument label 'onNext:' in call

更新 2:

init(login: Observable<String>, password: Observable<String>) {
// each time the login and password change, returns login and string value
didTapLoginButton = { [weak self] _ in
// allows for strong self reference
guard let `self` = self else { return }

Observable.combineLatest(login, password) { (login, password) in
//
}
.subscribe(onNext: { response in
self.makeLoginRequest(userLogin: login, loginPassword: password, loginSecret: self.loginSecret, deviceToken: self.deviceToken)
// do something with your response
})
.disposed(by: self.disposeBag)

}
}

最佳答案

添加.subscribe。在有订阅者之前,可观察对象不会发送事件。

例子:

let observable = Observable.from([1,2,3,4,5,6]) // won't send events. It's only the declaration. 

当您订阅它时,事情会发生:

observable.subscribe(onNext: { each in 
print(each) // Will print each number
})
.disposed(by: disposeBag) // add a dispose bag

我没有尝试过,但在您的特定情况下,我会这样做:

1- 在您的 viewModel 中,添加一个 clousure 属性:

var didTapLoginButton: () -> Void = { _ in }

2- 在您的 viewController - viewDidLoad() 方法中:

self.loginView.loginButton.rx.tap.asObservable()
.subscribe(onNext: { [weak self] _ in
self?.viewModel.didTapLoginButton()
})
.disposed(by: bag)

3- 在您的 viewModel 中:

init(login: Observable<String>, password: Observable<String>) {

didTapLoginButton = { [weak self] _ in
// This let you call self as strong reference instead optional
guard let `self` = self else { return }

Observable.combineLatest(login, password) { (login, password) -> (String, String) in
self.makeLoginRequest(userLogin: login, loginPassword: password, loginSecret: self.loginSecret, deviceToken: self.deviceToken)
}
.subscribe(onNext: { response in
// do something with your response
})
.disposed(by: bag)

}
}

关于ios - rxSwift 按钮按下观察器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45940947/

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