Latest 1.0.0
Homepage https://github.com/nixzhu/KeypathObserver
License MIT
Platforms ios 8.0, osx 10.10, tvos 9.0, watchos 2.0, requires ARC
Authors

KeypathObserver
KeypathObserver

KVO observing with simple syntax.

Requirements

Swift 3.0, iOS 8.0

(Swift 2.3, use version 0.6.0)

Example

class ViewController: UIViewController {

    var view: UIView?
    var centerObserver: KeypathObserver<UIView, CGPoint>?
    var colorObserver: KeypathObserver<UIView, UIColor>?

    override func viewDidLoad() {
        super.viewDidLoad()

        let view = UIView()

        self.centerObserver = KeypathObserver(
            object: view,
            keypath: #keyPath(UIView.center),
            valueTransformer: { ($0 as? NSValue)?.cgPointValue },
            valueChanged: { oldCenter, newCenter in
                print("oldCenter: (oldCenter)")
                print("newCenter: (newCenter)")
            }
        )

        view.center = CGPoint(x: 100, y: 100)
        view.center = CGPoint(x: 200, y: 50)

        self.colorObserver = KeypathObserver(
            object: view,
            keypath: #keyPath(UIView.backgroundColor),
            valueTransformer: { $0 as? UIColor },
            valueUpdated: { newColor in
                print("newColor: (newColor)")
            }
        )

        view.backgroundColor = UIColor.redColor()
        view.backgroundColor = nil

        self.view = view
    }
}

Installation

Carthage

github "nixzhu/KeypathObserver" >= 1.0.0

CocoaPods

pod 'KeypathObserver', '~> 1.0.0'

Contact

NIX @nixzhu

License

KeypathObserver is available under the MIT License. See the LICENSE file for more info.

Latest podspec

{
    "name": "KeypathObserver",
    "version": "1.0.0",
    "summary": "Make KVO observing easier.",
    "description": "KVO observing with simple syntax.",
    "homepage": "https://github.com/nixzhu/KeypathObserver",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "nixzhu": "[email protected]"
    },
    "social_media_url": "https://twitter.com/nixzhu",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "source": {
        "git": "https://github.com/nixzhu/KeypathObserver.git",
        "tag": "1.0.0"
    },
    "source_files": [
        "Sources/*.swift",
        "KeypathObserver/KeypathObserver.h"
    ],
    "public_header_files": [
        "KeypathObserver/KeypathObserver.h"
    ],
    "requires_arc": true
}

Pin It on Pinterest

Share This