RxSwift extentions for Swift optionals and "Occupiable" types

Overview

RxOptional

Build Status Version License Platform

RxSwift extentions for Swift optionals and "Occupiable" types.

Usage

All operators are available on Driver as well unless otherwise marked.

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
fatalErrorOnNil

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

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

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 always conform custom types to Occupiable.

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

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

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

.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

Running Examples.playground

  • 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

Requirements

Installation

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

pod "RxOptional"

Author

Thane Gill, [email protected]

License

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

You might also like...
Handy RxSwift extensions on NSObject, including rx.disposeBag.
Handy RxSwift extensions on NSObject, including rx.disposeBag.

NSObject+Rx If you're using RxSwift, you've probably encountered the following code more than a few times. class MyObject: Whatever { let disposeBag

RxSwift extensions for Core Data

RxCoreData Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Xcode 9.0 Swift 4.0

Support Combine Assign subscriber in RxSwift.

RxAssign Support Combine Assign subscriber in RxSwift. Assign uses a KeyPath which is really nice and useful. ** RxAssign extends Driver and Signal in

This is the demo of MVVM-C structure with dependency injection using RxSwift.
This is the demo of MVVM-C structure with dependency injection using RxSwift.

MVVMC-Demo Demo defination This is the demo project, I have integrated two APIs for MovieDB APIS (https://www.themoviedb.org/). One for the listing of

Unidirectional flow implemented using the latest Swift Generics and Swift Concurrency features.

swift-unidirectional-flow Unidirectional flow implemented using the latest Swift Generics and Swift Concurrency features. struct SearchState: Equatabl

Dynamic and type-safe framework for building linear and non-linear flows.

FlowKit FlowKit is a dynamic flow framework capable of building a flow, based on conditions and ordered according to a logic of next steps. By using F

Simple and lightweight Functional Reactive Coding in Swift for the rest of us
Simple and lightweight Functional Reactive Coding in Swift for the rest of us

The simplest ObservableT implementation for Functional Reactive Programming you will ever find. This library does not use the term FRP (Functional R

Lightweight observations and bindings in Swift
Lightweight observations and bindings in Swift

What is Hanson? Hanson is a simple, lightweight library to observe and bind values in Swift. It's been developed to support the MVVM architecture in o

Unidirectional State Management Architecture for Swift - Inspired by Vuex and Flux
Unidirectional State Management Architecture for Swift - Inspired by Vuex and Flux

Unidirectional State Management Architecture for Swift - Inspired by Vuex and Flux Introduction VueFlux is the architecture to manage state with unidi

Owner
Thane Gill
Thane Gill
Realm RxSwift - This application was written in order to use Realm, RxSwift frameworks in real example

Realm_RxSwift This simple app was written to introduce basic operations of some

Elbek Khasanov 3 Apr 7, 2022
🤖 RxSwift + State Machine, inspired by Redux and Elm.

RxAutomaton RxSwift port of ReactiveAutomaton (State Machine). Terminology Whenever the word "signal" or "(signal) producer" appears (derived from Rea

Yasuhiro Inami 719 Nov 19, 2022
STDevRxExt contains some extension functions for RxSwift and RxCocoa which makes our live easy.

STDevRxExt Example To run the Example.playground, clone the repo, and run pod install from the Example directory first. Requirements iOS 9.0+ tvOS 9.0

STDev 6 Mar 26, 2021
RxAlamoRecord combines the power of the AlamoRecord and RxSwift libraries to create a networking layer that makes interacting with API's easier than ever reactively.

Written in Swift 5 RxAlamoRecord combines the power of the AlamoRecord and RxSwift libraries to create a networking layer that makes interacting with

Dalton Hinterscher 9 Aug 7, 2020
RxSwift wrapper around the elegant HTTP networking in Swift Alamofire

RxAlamofire RxAlamofire is a RxSwift wrapper around the elegant HTTP networking in Swift Alamofire. Getting Started Wrapping RxSwift around Alamofire

RxSwift Community 1.6k Jan 3, 2023
Super Simple Pager with RxSwift extension

SSPager Super Simple Pager Example To run the example project, clone the repo, and run pod install from the SSPagerExample directory first. Installati

9oya 4 Jul 10, 2022
RxSwift bindings for Permissions API in iOS.

RxPermission RxSwift bindings for Permission API that helps you with Permissions in iOS. Installation RxPermission is available through CocoaPods. I c

Luke 230 Dec 27, 2022
A testable RxSwift wrapper around MultipeerConnectivity

A testable RxSwift wrapper around MultipeerConnectivity RxMultipeer is a RxSwift wrapper for MultipeerConnectivity. Using the adapter pattern, we can

RxSwift Community 69 Jul 5, 2022
iOS & OSX Bluetooth library for RxSwift

RxBluetoothKit is a Bluetooth library that makes interaction with BLE devices much more pleasant. It's backed by RxSwift and CoreBluetooth and it prov

Polidea 1.3k Dec 16, 2022
RxSwift reactive wrapper for view gestures

RxGesture Usage To run the example project, clone the repo, in the Example folder open RxGesture.xcworkspace. You might need to run pod install from t

RxSwift Community 1.3k Dec 30, 2022