Skip to content

timbodeit/ObserverManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ObserverManager

CI Status Version License Platform

Closure Support and automatic deregistering for Key Value Observing in Swift.

Requirements

This version of ObserverManager is meant to be used with Swift 3.0

For Swift 2 use Version 0.1.0

Usage

import Foundation
import ObserverManager

class SomeObservingClass {

    let foo: Foo
    let observerManager = ObserverManager()

    init(foo: Foo) {
        self.foo = foo
        
        observerManager.registerObserver(object: foo, keyPath: "bar") { bar in
        	// Do stuff
        	[...]
        }
    }
}

Interface

/**
A class that can be used to sign up for Key-Value Observing by
passing a Swift closure.

Every object, that uses KVO should have its own NotificationManager.
All observers are automatically deregistered when the object is deallocated.
*/
public class ObserverManager : NSObject {

/**
Registers a new observer for a given object and keypath.

- parameter object:  The object to observe
- parameter keyPath: The keyPath to observe
- parameter block:   The block that is called when the value changed. Gets called with the new value.
*/
open func registerObserver(object: NSObject, keyPath: String, block: @escaping (_ value: NSObject) -> ())


/**
Removes all observers that observe the given keypath on the given object.
*/
open func deregisterObservers(object: NSObject, keyPath: String)

/**
Removes all observers that observe any keypath on the given object.
*/
open func deregisterObservers(object: NSObject)

/**
Removes all observers that observe any keypath on any object.
*/
open func deregisterAllObservers()

}

Installation

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

pod "ObserverManager"

Author

Tim Bodeit, [email protected]

License

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