sakiwei / RxOptional

RxSwift extensions for Swift optionals and "Occupiable" types

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RxOptional

Build Status Version License Platform

RxSwift extensions for Swift optionals and "Occupiable" types.

Usage

All operators are also available on Driver, 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
distinctUntilChanged
Observable<Int?>
    .of(5, 6, 6, nil, nil, 3)
    .distinctUntilChanged()
    .subscribe { print($0) }
Next(Optional(5))
Next(Optional(6))
Next(nil)
Next(Optional(3))
Completed

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 to Occupiable.

filterEmpty
Observable<[String]>
    .of(["Single Element"], [], ["Two", "Elements"])
    .filterEmpty()
    .subscribe { print($0) }
Next(["Single Element"])
Next(["Two", "Elements"])
Completed
errorOnEmpty

Unavailable on Driver, because Drivers cannot error out.

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
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

Running Examples.playground

  • Run pod install in Example directory
  • Select RxOptional Examples target
  • Build
  • Show Debug Area (cmd+shift+Y)
  • Click blue play button in Debug Area

Requirements

Installation

RxOptional is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RxOptional'

Author

Thane Gill, me@thanegill.com

License

RxOptional is available under the MIT license. See the LICENSE file for more info.

About

RxSwift extensions for Swift optionals and "Occupiable" types

License:MIT License


Languages

Language:Swift 86.5%Language:Ruby 12.0%Language:Objective-C 1.5%