RxSwift extentions for Swift optionals and "Occupiable" types.
All operators are available on Driver as well unless otherwise marked.
Observable<String?>
.of("One", nil, "Three")
.filterNil() // Type is now Observable<String>
.subscribe { print($0) }
Next(One)
Next(Three)
Completed
Observable<String?>
.of("One", nil, "Three")
.replaceNilWith("Two") // Type is now Observable<String>
.subscribe { print($0) }
Next(One)
Next(Two)
Next(Three)
Completed
During release builds fatalErrors are logged. Durring Debug builds
.fatalErrorOnNil()
sends Error event for Observables and Driver
continutes after logging fatalError.
Observable<String?>
.of("One", nil, "Three")
.fatalErrorOnNil()
.subscribe { print($0) }
Next(One)
fatal Error: Found nil while trying to unwrap type <Optional<String>>
Error(Found nil while trying to unwrap type <Optional<String>>)
Unavailable on Driver. By default errors with
RxOptionalError.FoundNilWhileUnwrappingOptional
.
Observable<String?>
.of("One", nil, "Three")
.errorOnNil()
.subscribe { print($0) }
Next(One)
Error(Found nil while trying to unwrap type <Optional<String>>)
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
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 always conform
custom types to Occupiable
.
Observable<[String]>
.of(["Single Element"], [], ["Two", "Elements"])
.filterEmpty()
.subscribe { print($0) }
Next(["Single Element"])
Next(["Two", "Elements"])
Completed
During release builds fatalErrors are logged. Durring Debug builds
.fatalErrorOnEmpty()
sends Error event for Observables and Driver
continutes after logging fatalError.
Observable<[String]>
.of(["Single Element"], [], ["Two", "Elements"])
.fatalErrorOnEmpty()
.subscribe { print($0) }
Next(["Single Element"])
fatal Error: Empty occupiable of type <Array<String>>
Error(Empty occupiable of type <Array<String>>)
By default errors with RxOptionalError.EmptyOccupiable
.
Observable<[String]>
.of(["Single Element"], [], ["Two", "Elements"])
.errorOnEmpty()
.subscribe { print($0) }
Next(["Single Element"])
Error(Empty occupiable of type <Array<String>>)
.catchOnEmpty
guarantees that the hander function returns a Observable or Driver with
non-empty elements by calling .errorOnEmpty
or .fatalErrorOnEmpty
respectfully.
Observable<[String]>
.of(["Single Element"], [], ["Two", "Elements"])
.catchOnEmpty {
return Observable<[String]>.just(["Not Empty"])
}
.subscribe { print($0) }
Next(["Single Element"])
Next(["Not Empty"])
Next(["Two", "Elements"])
Completed
- Run
pod install
in Example directory - Select RxOptional Examples Target
- Build
- Open Examples.playground
- Show Debug Area (cmd+shit+Y)
- Click blue play button in Debug Area
RxOptional is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "RxOptional"
Thane Gill, me@thanegill.com
RxOptional is available under the MIT license. See the LICENSE file for more info.