Handy Combine extensions on NSObject, including Set.

Overview

Storable

image

Description

If you're using Combine, you've probably encountered the following code more than a few times.

class Object: NSObject {
    var cancellables = Set<AnyCancellable>()

    ...
}

Instead of adding a new property to every object, use this library to add it for you, to any subclass of NSObject.

Example1

final class Object: NSObject, Storable { // import Storable

    // You don't have to write this.
    // private var cancellables = Set<AnyCancellable>()

    func exec() {
        Just<Int>(0)
            .sink(receiveCompletion: { _ in
                // do something
            }, receiveValue: { _ in 
                // do something
            })
            .store(in: &self.cancellables) // read Set<AnyCancellable> in yourself.
    }
}

You can read Set<AnyCancellable> without member variable. It'll work just like a property: when the instance is deinit'd, the Set<AnyCancellable> gets disposed. It's also a readwrite property, so you can use your own, too.


Furthermore, .store(in: &self.cancellables) can be omitted.

Example2

final class Object: NSObject, Storable {

    func exec() {
        Just<Int>(0)
            .sink(self, receiveCompletion: { _ in // input self
                // do something
            }, receiveValue: { _ in 
                // do something
            })
    }
}

For a detailed explanation, please read here.

Installing

CocoaPods

Add the following line to your Podfile:

pod 'CombineStorable', :git => '[email protected]:crane-hiromu/CombineStorable.git', :branch => 'main'

Carthage

Add the following to your Cartfile:

github "crane-hiromu/CombineStorable"

Supplement

If you fail to install, reset caches. (rm -rf ~/Library/Caches/)

Swift Package Manager

Add the following dependency to your Package.swift file:

.package(url: "https://github.com/crane-hiromu/CombineStorable.git", from: "1.1.0")

License

MIT, of course ;-) See the LICENSE file.

You might also like...
Helpful extensions for iOS app development 🚀
Helpful extensions for iOS app development 🚀

ExtensionKit includes many extensions, from getting the user location with a deterministic Combine API to a shimmer loading animation, to keyboard notification updates, bottom sheet and much much more. Check out the docs below or install the library with SPM to try it out.

Extensions that allow you to work with optionals

RxOptionals Sometimes it happens that you need to bind to several optional binders or to an optional method (for example, when using weak). To do this

Useful Swift code samples, extensions, functionalities and scripts to cherry-pick and use in your projects

SwiftyPick 🦅 🍒 Useful Swift code samples, extensions, functionalities and scripts to cherry-pick and use in your projects. Purpose The idea behind t

Useful extensions for my Swift code
Useful extensions for my Swift code

UIViewController extensions presentAlert(withTitle title: String, message : String) presentAlertDialog(withTitle title: String, message : String, acti

Steps and files needed to reproduce a CSP bug in Safari Web Extensions

CSP Safari bug repro There appears to be a discrepancy between how Safari handles CSP policies for extension pages compared to how other browsers do s

Personally useful Swift Extensions for iOS Development

Useful-Swift-Extensions Personally useful Swift Extensions for iOS Development; cobbled together from a variety of development projects and StackOverf

Swift extensions for asynchronous CloudKit record processing

⛅️ AsyncCloudKit Swift extensions for asynchronous CloudKit record processing. D

Easier sharing of structured data between iOS applications and share extensions
Easier sharing of structured data between iOS applications and share extensions

XExtensionItem XExtensionItem is a tiny library allowing for easier sharing of structured data between iOS applications and share extensions. It is ta

Extensions for Swift Standard Types and Classes

Cent Cent is a library that extends certain Swift object types using the extension feature and gives its two cents to Swift language. Dollar is a Swif

Comments
Releases(1.0.0)
Owner
hcrane
iOS Developer.
hcrane
SharkUtils is a collection of Swift extensions, handy methods and syntactical sugar that we use within our iOS projects at Gymshark.

SharkUtils is a collection of Swift extensions, handy methods and syntactical sugar that we use within our iOS projects at Gymshark.

Gymshark 1 Jul 6, 2021
Extendy - A set of useful string extensions.

Extendy A set of useful string extensions. Requirements iOS 11.0+ Swift 5+ Installation CocoaPods Extendy is available through CocoaPods. To install i

Anton Novichenko 3 Sep 23, 2021
🌤 Swift Combine extensions for asynchronous CloudKit record processing

Swift Combine extensions for asynchronous CloudKit record processing. Designed for simplicity.

Chris Araman 46 Dec 8, 2022
A handy collection of Swift method and Tools to build project faster and more efficient.

SwifterKnife is a collection of Swift extension method and some tools that often use in develop project, with them you might build project faster and

李阳 4 Dec 29, 2022
TypeStyle is a handy app for iPhone and iPad that generates text using different styles and decorations. It is a native Swift iOS app.

TypeStyle TypeStyle is a handy app for iPhone and iPad that generates text using different styles and decorations. It is a native Swift iOS app. Featu

Eugene Belinski 31 Dec 14, 2022
Testable Combine Publishers - An easy, declarative way to unit test Combine Publishers in Swift

Testable Combine Publishers An easy, declarative way to unit test Combine Publishers in Swift About Combine Publishers are notoriously verbose to unit

Albert Bori 6 Sep 26, 2022
BCSwiftTor - Opinionated pure Swift controller for Tor, including full support for Swift 5.5 and Swift Concurrency

BCSwiftTor Opinionated pure Swift controller for Tor, including full support for

Blockchain Commons, LLC — A “not-for-profit” benefit corporation 4 Oct 6, 2022
BFKit-Swift is a collection of useful classes, structs and extensions to develop Apps faster.

Features • Classes and Extensions Compatibility • Requirements • Communication • Contributing • Installing and Usage • Documentation • Changelog • Exa

Fabrizio Brancati 992 Dec 2, 2022
Extensions giving Swift's Codable API type inference super powers 🦸‍♂️🦹‍♀️

Welcome to Codextended — a suite of extensions that aims to make Swift’s Codable API easier to use by giving it type inference-powered capabilities an

John Sundell 1.4k Jan 2, 2023
A Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and other native frameworks.

ZamzamKit ZamzamKit is a Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and othe

Zamzam Inc. 261 Dec 15, 2022