Skip to content

bofrese/ObserverManager

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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 2.0

Usage

import Foundation
import ObserverManager

class SomeObservingClass {

    let foo: Foo
    let observerManager = ObserverManager()

    init(foo: Foo) {
        self.foo = foo
        
        observerManager.registerObserverForObject(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.
*/
public func registerObserverForObject(object: NSObject, keyPath: String, block: (value: NSObject) -> ())

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

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

/**
Removes all observers that observe any keypath on any object.
*/
public 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.

About

Make KVO suck less.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 83.7%
  • Ruby 16.3%