Latest 0.0.7
Homepage https://github.com/peterdeweese/Observative
License MIT
Platforms ios 9.0, osx 10.10, requires ARC
Authors

Swift library to simplify key value observation. KVO only works with NSObjects, so Observative can only observe ancestors of NSObject.

  • Observe properties only on NSObjects
  • Declare observed properties dynamic, dynamic var myVar

Methods

  • observe(_ object:NSObject, keyPath:String, onChange:see below)
    • ( oldValue:Any?, newValue:Any?) -> Void
    • (_ newValue:Any?) -> Void
    • () -> Void
  • stopObserving(_ object:NSObject)
  • stopObserving()

Playground Example

var updateCalled = false
let objectToWatch = NSMutableDictionary(dictionary:["key1":"value1"])

let observative = Observative()
observative.observe(objectToWatch, keyPath:"key1")
{
    updateCalled = true
}

objectToWatch["key1"] = "updatedValue1"
if updateCalled{ print("it worked!") }

Class Example

class MyController, Observes
{
    var objectToWatch:CustomObject {
        didSet {
            if oldValue != nil
            {
                stopObserving(oldValue)
            }
            observe(objectToWatch, keyPath:"myProperty")
            {
                self.doOnChange()
            }
        }
    }

    func doOnChange()
    {
        print("myProperty changed!")
        // object changed
    }
}

Latest podspec

{
    "name": "Observative",
    "version": "0.0.7",
    "summary": "Observative tracks NSObject changes using custom blocks and remembers what it is observing.",
    "homepage": "https://github.com/peterdeweese/Observative",
    "license": "MIT",
    "authors": {
        "Peter DeWeese": "[email protected]"
    },
    "requires_arc": true,
    "source": {
        "git": "https://github.com/peterdeweese/Observative.git",
        "tag": "0.0.7"
    },
    "source_files": "lib/*.{h,m,swift}",
    "public_header_files": "lib/*.h",
    "platforms": {
        "ios": "9.0",
        "osx": "10.10"
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This