什么是RxSwift 3.0相当于RxSwift 2.x的AnonymousDisposable?

时间:2016-12-02 16:01:27

标签: swift swift3 rx-swift

ReactiveX.io documentation使用AnonymousDisposable,它位于RxSwift 2.x中,但是在RxSwift 3.0中是未解析的标识符。我该怎么用?

let source = Observable.create { observer in
  for i in 1...5 {
    observer.on(.Next(i))
  }
  observer.on(.Completed)

  // Note that this is optional. If you require no cleanup you can return
  // NopDisposable.instance
  return AnonymousDisposable {
    print("Disposed")
  }
}

source.subscribe {
  print($0)
}

2 个答案:

答案 0 :(得分:1)

请注意Swift 2中的这种语法:

NopDisposable.instance

也被替换为

Disposables.create() 

除此之外,有趣的是,在引擎盖下,NopDisposable仍然存在但通过此create方法公开。 Here's the source

答案 1 :(得分:0)

使用:

def compute(bigrams, table):
    """Yield a resultant operation for each bigram."""
    for bigram in bigrams:
        # Get values and convert strings 
        x = int(table[bigram[0]])
        y = int(table[bigram[1]])
        z = int(bigram[2])

        operation = (x * y) / z
        yield operation


list(compute(list_2, dict(list_1)))
# [12.5, 15.0]