https://github.com/RxSwiftCommunity/RxOptional
RxSwift extensions for Swift optionals and "Occupiable" types
https://github.com/RxSwiftCommunity/RxOptional
Last synced: 3 months ago
JSON representation
RxSwift extensions for Swift optionals and "Occupiable" types
- Host: GitHub
- URL: https://github.com/RxSwiftCommunity/RxOptional
- Owner: RxSwiftCommunity
- License: mit
- Created: 2016-02-04T07:47:28.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2024-10-06T19:19:58.000Z (5 months ago)
- Last Synced: 2024-10-24T03:34:42.877Z (4 months ago)
- Language: Swift
- Homepage:
- Size: 261 KB
- Stars: 700
- Watchers: 31
- Forks: 93
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-rxswift - RxOptional
- awesome - RxOptional - RxSwift extensions for Swift optionals and "Occupiable" types (OOM-Leaks-Crash / Rx Series)
- awesome - RxOptional - RxSwift extensions for Swift optionals and "Occupiable" types (OOM-Leaks-Crash / Rx Series)
README
# RxOptional
[](https://circleci.com/gh/RxSwiftCommunity/RxOptional/tree/master)
[](http://cocoapods.org/pods/RxOptional)
[](http://cocoapods.org/pods/RxOptional)
[](http://cocoapods.org/pods/RxOptional)RxSwift extensions for Swift optionals and "Occupiable" types.
## Usage
All operators are also available on `Driver` and `Signal`, unless otherwise noted.
### Optional Operators
##### filterNil
```swift
Observable
.of("One", nil, "Three")
.filterNil()
// Type is now Observable
.subscribe { print($0) }
``````text
next(One)
next(Three)
completed
```##### replaceNilWith
```swift
Observable
.of("One", nil, "Three")
.replaceNilWith("Two")
// Type is now Observable
.subscribe { print($0) }
``````text
next(One)
next(Two)
next(Three)
completed
```##### errorOnNil
Unavailable on `Driver`, because `Driver`s cannot error out.
By default errors with `RxOptionalError.foundNilWhileUnwrappingOptional`.
```swift
Observable
.of("One", nil, "Three")
.errorOnNil()
// Type is now Observable
.subscribe { print($0) }
``````text
next(One)
error(Found nil while trying to unwrap type >)
```##### catchOnNil
```swift
Observable
.of("One", nil, "Three")
.catchOnNil {
return Observable.just("A String from a new Observable")
}
// Type is now Observable
.subscribe { print($0) }
``````text
next(One)
next(A String from a new Observable)
next(Three)
completed
```##### distinctUntilChanged
```swift
Observable
.of(5, 6, 6, nil, nil, 3)
.distinctUntilChanged()
.subscribe { print($0) }
``````text
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
```swift
Observable<[String]>
.of(["Single Element"], [], ["Two", "Elements"])
.filterEmpty()
.subscribe { print($0) }
``````text
next(["Single Element"])
next(["Two", "Elements"])
completed
```##### errorOnEmpty
Unavailable on `Driver`, because `Driver`s cannot error out.
By default errors with `RxOptionalError.emptyOccupiable`.
```swift
Observable<[String]>
.of(["Single Element"], [], ["Two", "Elements"])
.errorOnEmpty()
.subscribe { print($0) }
``````text
next(["Single Element"])
error(Empty occupiable of type >)
```##### catchOnEmpty
```swift
Observable<[String]>
.of(["Single Element"], [], ["Two", "Elements"])
.catchOnEmpty {
return Observable<[String]>.just(["Not Empty"])
}
.subscribe { print($0) }
``````text
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
- [RxSwift](https://github.com/ReactiveX/RxSwift)
- [RxCocoa](https://github.com/ReactiveX/RxSwift)## Installation
### [CocoaPods](https://guides.cocoapods.org/using/using-cocoapods.html)
RxOptional is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'RxOptional'
```### [Carthage](https://github.com/Carthage/Carthage)
Add this to `Cartfile`
```
github "RxSwiftCommunity/RxOptional" ~> 4.1.0
``````
$ carthage update
```### [Swift Package Manager](https://swift.org/package-manager)
To use RxOptional as a Swift Package Manager package just add the following in your Package.swift file.
```swift
import PackageDescriptionlet package = Package(
name: "ProjectName",
dependencies: [
.Package(url: "https://github.com/RxSwiftCommunity/RxOptional")
]
)
```## Author
Thane Gill
## License
RxOptional is available under the MIT license. See the LICENSE file for more info.