diff --git a/OSXPlayground.playground/Contents.swift b/OSXPlayground.playground/Contents.swift index 7c17371f..9459607a 100644 --- a/OSXPlayground.playground/Contents.swift +++ b/OSXPlayground.playground/Contents.swift @@ -101,6 +101,27 @@ example("returnElements") { With the above, you will see that the `.Next` event was sent ten times, once for each element. Then `.Complete` was sent. */ +/*: +### from +We can also create an observable from any SequenceType, such as an array +*/ + +example("from") { + let fromArrayObservable = from([1, 2, 3, 4, 5]) + + let fromArrayObservableSubscriber = fromArrayObservable + >- subscribe { event in + switch event { + case .Next(let box): + println("\(box.value)") + case .Completed: + println("completed") + case .Error(let error): + println("\(error)") + } + } +} + /*: Now these functions are all well and good, but the really useful ones are in the RxCocoa library. `rx_observe` exist on every NSObject and wraps KVO. diff --git a/RxSwift/RxSwift/Observables/Observable+Creation.swift b/RxSwift/RxSwift/Observables/Observable+Creation.swift index 8fee8ae8..c27ac0f8 100644 --- a/RxSwift/RxSwift/Observables/Observable+Creation.swift +++ b/RxSwift/RxSwift/Observables/Observable+Creation.swift @@ -56,6 +56,17 @@ public func returnElements(values: E ...) -> Observable { } } +public func from(sequence: S) -> Observable { + return AnonymousObservable { observer in + for element in sequence { + sendNext(observer, element) + } + + sendCompleted(observer) + return DefaultDisposable() + } +} + // fail public func failWith(error: ErrorType) -> Observable {