参考链接: http://blog.csdn.net/Hello_Hwc/article/details/51859330
官方地址:https://github.com/ReactiveX/RxSwift
demo解读:
一、number
Observable.combineLatest(number1.rx.text, number2.rx.text, number3.rx.text) { textValue1, textValue2, textValue3 -> Int in
return (Int(textValue1) ?? 0) + (Int(textValue2) ?? 0) + (Int(textValue3) ?? 0)
}
.map { $0.description }
.bindTo(result.rx.text)
.addDisposableTo(disposeBag)
名词释义:
1.Observable
2.combineLatest
3.??
4.map
5.bingTo
6.addDisposableTo
7.disposeBag
二、valiadation
let myUserNameValid = usernameOutlet.rx.text
.map{$0.characters.count > 6}
.shareReplay(1)
let myPasswordValid = passwordOutlet.rx.text
.map{$0.characters.count > 6}
.shareReplay(1)
let myEverythingValid = Observable.combineLatest(usernameValid, passwordValid) {$0 && $1}
.shareReplay(1)
myUserNameValid
.bindTo(usernameValidOutlet.rx.hidden)
.addDisposableTo(disposeBag)
myPasswordValid
.bindTo(passwordValidOutlet.rx.hidden)
.addDisposableTo(disposeBag)
myEverythingValid
.bindTo(doSomethingOutlet.rx.enabled)
.addDisposableTo(disposeBag)
doSomethingOutlet.rx.tap
.subscribe(onNext: {[weak self] in self?.showAlert()})
.addDisposableTo(disposeBag)
名词释义:
1.shareReplay(1)
2.subScribe(onNext:{[weak seld] in self?.showAlert() })
三、Geolocation Subscription
private extension Reactive where Base: UILabel {
var driveCoordinates: AnyObserver<CLLocationCoordinate2D> {
return UIBindingObserver(UIElement: base) { label, location in
label.text = "Lat: \(location.latitude)\nLon: \(location.longitude)"
}.asObserver()
}
}
private extension Reactive where Base: UIView {
var driveAuthorization: AnyObserver<Bool> {
return UIBindingObserver(UIElement: base) { view, authorized in
if authorized {
view.isHidden = true
view.superview?.sendSubview(toBack:view)
}
else {
view.isHidden = false
view.superview?.bringSubview(toFront:view)
}
}.asObserver()
}
}
let geolocationService = GeolocationService.instance
geolocationService.authorized
.drive(noGeolocationView.rx.driveAuthorization)
.addDisposableTo(disposeBag)
geolocationService.location
.drive(label.rx.driveCoordinates)
.addDisposableTo(disposeBag)
button.rx.tap
.bindNext { [weak self] in
self?.openAppPreferences()
}
.addDisposableTo(disposeBag)
button2.rx.tap
.bindNext { [weak self] in
self?.openAppPreferences()
}
.addDisposableTo(disposeBag)
}
private func openAppPreferences() {
UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
}
名词释义:
1.drive
2.asObserver