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

评论

0条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注