If you're using Combine
, you've probably encountered the following code more than a few times.
class MyObject: Whatever {
var cancellable: Set<AnyCancellable>
...
}
You're actually not the only one; it has been typed many, many times.
Instead of adding a new property to every object, use this library to add it for you, to any subclass of NSObject
.
thing
.sink { ... }
.store(in: &cancellable)
Sweet.
It'll work just like a property: when the instance is deinit'd, the Cancellable
gets disposed. It's also a read/write property, so you can use your own, too.
If you want to add a Cancellable to an Object that does not inherit from NSObject, you can also implement the protocol HasCancellable
, and you're good to go. This protocol provides a default Cancellable called cancellable
.
Add to your Podfile
:
pod 'NSObject+Combine'
And that'll be 👌
Source files are in the root directory. We use CocoaPods to develop, check out the unit tests in the Demo project.
MIT obvs.
link |
Stars: 1 |
Last commit: 2 years ago |
Fix crash
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics