RxSwiftMap与FastMap区别
我们来看看map
函数和flatMap
函数的定义:
map
函数,接收一个 R 类型的序列,返回一个 R 类型的序列,还是原来的序列。
public func map<R>(_ transform: @escaping (Self.E) throws -> R) -> RxSwift.Observable<R>
public func flatMap<O: ObservableConvertibleType>(_ selector: @escaping (E) throws -> O)
-> Observable<O.E>
let disposeBag = DisposeBag()
let observable = Observable.of("1","2","3","4","5").map{$0 + "scott"}
observable.subscribe(onNext: {print($0)}).disposed(by: disposeBag)
1scott
2scott
3scott
4scott
5scott
let test = Observable.of("1", "2", "3", "4", "5")
.map { Observable.just($0) }
test.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
RxSwift.(Just in _BD9B9D4356C4038796FB16D0D54A9F8E)<Swift.String>
RxSwift.(Just in _BD9B9D4356C4038796FB16D0D54A9F8E)<Swift.String>
RxSwift.(Just in _BD9B9D4356C4038796FB16D0D54A9F8E)<Swift.String>
RxSwift.(Just in _BD9B9D4356C4038796FB16D0D54A9F8E)<Swift.String>
RxSwift.(Just in _BD9B9D4356C4038796FB16D0D54A9F8E)<Swift.String>
let test = Observable.of("1", "2", "3", "4", "5")
.map { Observable.just($0) }.merge()
test.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
1
2
3
4
5
let test = Observable.of("1", "2", "3", "4", "5")
.flatMap { Observable.just($0) }
test.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
1
2
3
4
5
发表回复