Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- final class DeallocNotifier {
- private var _observers: [() -> Void] = []
- func observe(_ observer: @escaping () -> Void) {
- _observers.append(observer)
- }
- deinit {
- _observers.forEach { observer in
- observer()
- }
- }
- }
- private var deallocNotifierContext: UInt8 = 0
- extension NSObject {
- var deallocNotifier: DeallocNotifier {
- /// This is a technique to use getter as if this object has additional property
- if let notifier = objc_getAssociatedObject(self, &deallocNotifierContext) as? DeallocNotifier {
- return notifier
- }
- let notifier = DeallocNotifier()
- objc_setAssociatedObject(self, &deallocNotifierContext, notifier, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- return notifier
- }
- }
Add Comment
Please, Sign In to add comment