Skip to content

Instantly share code, notes, and snippets.

@gravicle
Created June 15, 2016 21:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravicle/8fd14b940e97e6d4bc7ecfec3703fd2e to your computer and use it in GitHub Desktop.
Save gravicle/8fd14b940e97e6d4bc7ecfec3703fd2e to your computer and use it in GitHub Desktop.
import Foundation
import RxSwift
protocol OptionalType {
associatedtype Wrapped
var value: Wrapped? { get }
}
extension Optional: OptionalType {
var value: Wrapped? {
return self
}
}
extension Observable where Element: OptionalType {
func filterNil() -> Observable<Element.Wrapped> {
return flatMap { (element) -> Observable<Element.Wrapped> in
if let value = element.value {
return .just(value)
} else {
return .empty()
}
}
}
}
@gravicle
Copy link
Author

Only works for observables which have optional values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment