I've having a problem in RxSwift in converting a Observable into a String.
I understand that an observable is a sequence, I just want the last change in the username.
I'm storing username and password as:
let username = BehaviorRelay<String>(value: "")
let password = BehaviorRelay<String>(value: "")
And previously have used combineLatest
func loginButtonValid(username: Observable<String>, password: Observable<String>) -> Observable<Bool> {
return Observable.combineLatest(username, password)
{ (username, password) in
return username.count > 0
&& password.count > 0
&& self.validateEmail(enteredEmail: username)
}
}
But how can I just take the latest from the username?
I've tried takelast and use combineLatest with just one argument, but neither seems to work.
I want to do this to validate whether an email is valid, and the validate email function I'm using is the following (for reference only):
func validateEmail(enteredEmail:String) -> Bool {
let emailFormat = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailPredicate = NSPredicate(format:"SELF MATCHES %@", emailFormat)
return emailPredicate.evaluate(with: enteredEmail)
}
My attempts are around the following ideas
func emailValid(username: Observable<String>) -> Observable<Bool> {
return username.takeLast()
{ (lastusername) in
return self.validateEmail(enteredEmail: (lastusername) )
}
}
If I understood correctly, you want to transform an Observable<String>
to an Observable<Bool>
, which is the job of the map
method:
func emailValid(username: Observable<String>) -> Observable<Bool> {
return username.map(self.validateEmail)
}