RxSwift extensions for Swift optionals and “Occupiable” types.
Usage
All operators are also available on Driver and Signal, unless otherwise noted.
Optional Operators
filterNil
Observable<String?>
.of("One", nil, "Three")
.filterNil()
// Type is now Observable<String>
.subscribe { print($0) }
next(One)
next(Three)
completed
replaceNilWith
Observable<String?>
.of("One", nil, "Three")
.replaceNilWith("Two")
// Type is now Observable<String>
.subscribe { print($0) }
next(One)
next(Two)
next(Three)
completed
errorOnNil
Unavailable on Driver, because Drivers cannot error out.
By default errors with RxOptionalError.foundNilWhileUnwrappingOptional.
Observable<String?>
.of("One", nil, "Three")
.errorOnNil()
// Type is now Observable<String>
.subscribe { print($0) }
next(One)
error(Found nil while trying to unwrap type <Optional<String>>)
catchOnNil
Observable<String?>
.of("One", nil, "Three")
.catchOnNil {
return Observable<String>.just("A String from a new Observable")
}
// Type is now Observable<String>
.subscribe { print($0) }
next(One)
next(A String from a new Observable)
next(Three)
completed
Currently in Swift protocols cannot be extended to conform to other protocols.
For now the types listed above conform to Occupiable. You can also conform
custom types to Occupiable.
RxOptional
RxSwift extensions for Swift optionals and “Occupiable” types.
Usage
All operators are also available on
Driver
andSignal
, unless otherwise noted.Optional Operators
filterNil
replaceNilWith
errorOnNil
Unavailable on
Driver
, becauseDriver
s cannot error out.By default errors with
RxOptionalError.foundNilWhileUnwrappingOptional
.catchOnNil
distinctUntilChanged
Occupiable Operators
Occupiables are:
String
Array
Dictionary
Set
Currently in Swift protocols cannot be extended to conform to other protocols. For now the types listed above conform to
Occupiable
. You can also conform custom types toOccupiable
.filterEmpty
errorOnEmpty
Unavailable on
Driver
, becauseDriver
s cannot error out.By default errors with
RxOptionalError.emptyOccupiable
.catchOnEmpty
Running Examples.playground
pod install
in Example directoryRequirements
Installation
CocoaPods
RxOptional is available through CocoaPods. To install it, simply add the following line to your Podfile:
Carthage
Add this to
Cartfile
Swift Package Manager
To use RxOptional as a Swift Package Manager package just add the following in your Package.swift file.
Author
Thane Gill, me@thanegill.com
License
RxOptional is available under the MIT license. See the LICENSE file for more info.