/KVOController-Swift

Generic, simple key-value observing for Swift

Primary LanguageSwiftMIT LicenseMIT

KVOController-Swift

Version License Platform

Supports Swift 3.0

Have you ever wondered if you can implement a generic key-value observing for Swift. It makes your life easy and saves you a lot of casting. This project is inspired by facebook/KVOController. So, it doesn't only provide a neat API for KVO', but also makes use of Swift generics feature.

Requirements

  • iOS 7.0+ / Mac OS X 10.9+
  • Xcode 8
  • Swift 3.0

Usage

Simple use

observe(retainedObservable: clock, keyPath: "date", options: .New | .Initial)
    { [weak self] (observable: Clock, change: ChangeData<NSDate>) -> () in

        if let date = change.newValue {
            self?.timeLabel.text = formatter.stringFromDate(date)
        }
}

Note, you have to give the change a generic type of the property. In our case property is NSDate, so change is of type ChangeData<NSDate>

You can make use of automatic unobserving behavior or if you really want to unobserve, simply call

  unobserve(clock, keyPath: "date")

Installation

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

pod "KVOController-Swift"

For iOS 7.x:

Embedded frameworks require a minimum deployment target of iOS 8.

To use KVOController-Swift with a project targeting iOS 7, you must include KVOController.swift directly into your project.

Author

Mohamed Afifi, mohamede1945@gmail.com

License

KVOController-Swift is available under the MIT license. See the LICENSE file for more info.