RxOptional Documentation

repository·main·Indexed 20 days ago

https://github.com/rxswiftcommunity/rxoptional

RxSwift extensions for handling Swift optionals and 'Occupiable' types—such as Arrays, Strings, and Dictionaries—within reactive streams. Provides operators for Observable, Driver, and Signal to filter, replace, or handle nil values and empty states.

Tokens
1.4K
Snippets
3
Records
4
Agent score
22%

What's inside RxOptional

  1. Install RxOptional

    main

    You can install RxOptional using CocoaPods, Carthage, or Swift Package Manager.

    ### CocoaPods
    ```ruby
    pod 'RxOptional'

    Carthage

    Add to Cartfile:

    github "RxSwiftCommunity/RxOptional" ~> 4.1.0

    Then run:

    $ carthage update

    Swift Package Manager

    Add to your Package.swift:

    import PackageDescription
    
    let package = Package(
        name: "ProjectName",
        dependencies: [
            .Package(url: "https://github.com/RxSwiftCommunity/RxOptional")
        ]
    )
  2. Use Optional Operators

    main

    RxOptional provides operators for Observable, Driver, and Signal to handle Swift optionals. These operators allow you to filter, replace, or handle errors when an optional value is nil.

    // filterNil: Removes nil values and unwraps the type
    Observable<String?>
        .of("One", nil, "Three")
        .filterNil()
        .subscribe { print($0) } // Outputs: One, Three
    
    // replaceNilWith: Replaces nil with a specific value
    Observable<String?>
        .of("One", nil, "Three")
        .replaceNilWith("Two")
        .subscribe { print($0) } // Outputs: One, Two, Three
    
    // errorOnNil: Errors if a nil is encountered. 
    // Note: Unavailable on Driver.
    Observable<String?>
        .of("One", nil, "Three")
        .errorOnNil()
        .subscribe { print($0) } // Errors with RxOptionalError.foundNilWhileUnwrappingOptional
    
    // catchOnNil: Provides a fallback Observable when nil is encountered
    Observable<String?>
        .of("One", nil, "Three")
        .catchOnNil {
            return Observable<String>.just("A String from a new Observable")
        }
        .subscribe { print($0) } // Outputs: One, A String from a new Observable, Three
    
    // distinctUntilChanged: Standard operator that works with optionals
    Observable<Int?>
        .of(5, 6, 6, nil, nil, 3)
        .distinctUntilChanged()
        .subscribe { print($0) } // Outputs: Optional(5), Optional(6), nil, Optional(3)
  3. Use Occupiable Operators

    main

    Occupiables are types that can be empty, such as String, Array, Dictionary, and Set. RxOptional provides operators to handle these empty states. You can also conform custom types to Occupiable.

    // filterEmpty: Removes empty occupiables from the stream
    Observable<[String]>
        .of(["Single Element"], [], ["Two", "Elements"])
        .filterEmpty()
        .subscribe { print($0) } // Outputs: ["Single Element"], ["Two", "Elements"]
    
    // errorOnEmpty: Errors if an empty occupiable is encountered. 
    // Note: Unavailable on Driver.
    Observable<[String]>
        .of(["Single Element"], [], ["Two", "Elements"])
        .errorOnEmpty()
        .subscribe { print($0) } // Errors with RxOptionalError.emptyOccupiable
    
    // catchOnEmpty: Provides a fallback Observable when an empty occupiable is encountered
    Observable<[String]>
        .of(["Single Element"], [], ["Two", "Elements"])
        .catchOnEmpty {
            return Observable<[String]>.just(["Not Empty"])
        }
        .subscribe { print($0) } // Outputs: ["Single Element"], ["Not Empty"], ["Two", "Elements"]