Latest 1.0.1
Homepage https://github.com/damouse/Silvery
License MIT
Platforms ios 8.0, osx 10.9, requires ARC
Authors

Silvery 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.setValue("John", forKey:"name")
} catch {
  print(error)
}

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

Installation

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

use_frameworks!
pod 'Silvery'

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

import Silvery

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 the protocol Model:

extension Person : Model {}

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.setValue("John", forKey: "name")
  try person.setValue(36, forKey: "age")
  if let id = try person.valueForKey("id") as? Int {
    print(id)
  }
} catch {
  print(error)
}

Be aware that every one of your model’s properties must conform to the protocol Property like so:

extension MyCustomType : Property {}

Implicitly unwrapped optional properties will not work properly and should be avoided:

struct User : Model {
  var id: String
  var email: String! // Will not work as expected and may result in a fatal error if set or accessed
}

There is another caveat when using classes. If your class inherits from a superclass with private properties, Silvery will not work as expected and may fail.

Author

Brad Hilton, [email protected]

License

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

Latest podspec

{
    "name": "Silvery",
    "version": "1.0.1",
    "summary": "Pure swift property introspection",
    "description": "Pure swift property introspection similar to Objective-C key-value coding.",
    "homepage": "https://github.com/damouse/Silvery",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Mickey Barboi": "[email protected]"
    },
    "source": {
        "git": "https://github.com/damouse/Silvery.git",
        "tag": "1.0.1"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    },
    "source_files": [
        "Silvery",
        "Silvery/**/*.{swift,h,m}"
    ],
    "requires_arc": true
}

Pin It on Pinterest

Share This