Swift笔记 - 18.响应式编程、Swift源码分析

Swift笔记 - 18.响应式编程、Swift源码分析

响应式编程

public enum Event<Element> {
    /// Next element is produced.
    case next(Element)
    /// Sequence terminated with an error.
    case error(Swift.Error)
    /// Sequence completed successfully.
    case completed
}
var observable = Observable«Int>.create { observer in
observer.onNext(1)
observer.onCompleted()
return Disposables.create()
}
//等价于
observable = Observable.just (1)
observable = Observable.of(1)
observable = Observable.from([1])
var observable = Observable<Int>.create { observer in
    observer.onNext(1)
    observer.onNext(2)
    observer.onNext(3)
    observer.onCompleted()
    return Disposables.create()
}
// 等价于
observable = Observable.of(1, 2, 3)
observable = Observable.from([1, 2, 31)
observable.subscribe { event
    print(event)
}.dispose()

observable. subscribe (onNext:
    print("next", $0)
}, .onError: {
    print("error", $0)
}, .onCompleted: {
    print("completed")
}, onDisposed: {
    print("dispose")
}).dispose()
let observable = Observable<Int>.timer(.seconds(2), period: .seconds(1), scheduler: MainScheduler. instance)
observable.map{ "数值是:\($0)" }.bind(to:label.rx.text)
let observable = Observable<Int.timer(.seconds(2), period: .seconds(1), scheduler:MainScheduler.instance)
let binder = Binder<Bool>(button){ button, value in
    button.isHidden = value
}
observable.map { $0 % 2 == 0 }.bind(to: binder).disposed(by: bag)
//立即取消订间(一次性订阅)
observable.subscribe { event in
    print(event)
}.dispose()

//当bag销毁 (deinit) 时,会自动调用Disposable实例的dispose
observable.subscribe { event in
    print (event)
}.disposed(by: bag)

// self销毁 (deinit) 时,会自动调用Disposable实例的dispose
let _ = observable.takeUntil(self.rx.deallocated).subscribe { event in
    print(event)
}
let observer = AnyObserver<Int>.init{ event in
    switch event {
        case .next(let data):
            print (data)
        case .completed:
            print ("completed")
        case .error(let error):
            print("error", error)
    }
}
Observable. just (1).subscribe(observer).dispose()


let binder = Binder<String>(label) { label, text in
label.text = text
Observable.just(1).map { "数值是\($0)" }.subscribe(binder).dispose()
Observable.just(1).map { "数值是\($0)" }.bind(to: binder).dispose)
extension Reactive where Base: UIControl
    var hidden: Binder<Bool> {
        Binder<Bool>(base) { button, value in
            button.isHidden = value
        }
    }
    var enabled: Binder<Bool> {
        Binder<Bool> (base) { button, value in
            bytton.isEnabled = value
        }
    }
}
struct Person {
var name: String
var age: Int
}
class ViewController: UIViewController {
    @IBOutlet weak var slider: UISlider!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var textField: UITektField!
    let bag = DisposeBag()
    let persons = Observable.just([
        Person(name: "Jack", age: 20),
        Person(name: "Rose", age: 22),
        Person(name: "Kate", age: 25)
    ])
    override func viewDidLoad() {
        super.viewDidLoad()
        persons.bind(to: tableView.rx.items(cellIdentifier: "cell")) 
              { row, person, cell in
            cell. textLabel?.text = person.name
            cell.detailTextlabel?.text = "\(person.age)"
        }.disposed(by: bag)
        
        tableView.rx.itemSelected.subscribe(onNext: { path in
            print("At7", path)
        }).disposed(by: bag)
        
        //tableView.rx.modelSelected(Person.self).subscribe (onNext: { person in
        //    print("At7", person)
        //}).disposed(by: bag)
    }
    
    func test2() {
        Observable.just (0.8).bind(to:
        slider.rx.value).disposed(by: bag)
        slider.rx.value.map ( "slider数值是\(S0)" ).bind(to: textfield.rx.text).disposed(by: bag)
    	
        textField.rx.text.subscribe(onNext: { text in
    	       print(text ?? "")
        }).disposed(by: bag)
    	
        button.rx.controlEvent(.touchUpInside).subscribe(onNext:
    	        print("按钮被点击了"
        )).disposed(py: bag)
    	
        let observable = Observable<Int>.timer(.seconds (2), period: .seconds(1), scheduler: MainScheduler.instance)
        let binder = Binder<Bool>(button)( button, value in
    	        button.isHidden = value
        }
        observable.map ( "\($0)" ).bind(to: 1abel.rx.text).disposed(by:bag)
    }
}

Swift源码分析

版权所有,转载请注明出处 luowei.github.io.