Latest 4.0.1
Homepage https://github.com/bradhilton/SwiftKVC
License MIT
Platforms ios 8.0, osx 10.9, requires ARC
Dependencies Reflection
Authors

SwiftKVC brings key-value coding to native Swift classes and structures. You can easily set and access properties just using a subscript:

var person = Person()
person["name"] = "John"

Or use the more verbose method to catch potential errors:

var person = Person()
do {
  try person.set(value: "John", key: "name")
} catch {
  print(error)
}

SwiftKVC brings the power of Cocoa style key-value coding to Swift.

Installation

SwiftKVC is available through CocoaPods. To install, simply include the following lines in your podfile:

use_frameworks!
pod 'SwiftKVC'

Be sure to import the module at the top of your .swift files:

import SwiftKVC

Alternatively, clone this repo or download it as a zip and include the classes in your project.

Usage

To enable key-value coding for a native Swift structure or class, simply have it conform to either Value or Object respectively:

struct Person : Value {
  var name: String
  var age: Int
}

You can then set and retrieve values from your model by key:

person["name"] = "John"
person["age"] = 36
if let id = person["id"] as? Int {
  print(id)
}

If you would like to handle possible errors, you can use the more verbose methods:

do {
  try person.set(value: "John", key: "name")
  try person.set(value: 36, key: "age")
  if let id = try person.get(key: "id") as? Int {
    print(id)
  }
} catch {
  print(error)
}

Author

Brad Hilton, [email protected]

License

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

Latest podspec

{
    "name": "SwiftKVC",
    "version": "4.0.1",
    "summary": "Key-Value Coding (KVC) for native Swift classes and structs",
    "description": "SwiftKVC enables Key-Value Coding (KVC) for native Swift classes and structs.",
    "homepage": "https://github.com/bradhilton/SwiftKVC",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Brad Hilton": "[email protected]"
    },
    "source": {
        "git": "https://github.com/bradhilton/SwiftKVC.git",
        "tag": "4.0.1"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    },
    "source_files": [
        "SwiftKVC",
        "SwiftKVC/**/*.{swift,h,m}"
    ],
    "requires_arc": true,
    "dependencies": {
        "Reflection": [
            "~> 0.16.0"
        ]
    }
}

Pin It on Pinterest

Share This