Skip to content

Instantly share code, notes, and snippets.

@bluwave
Forked from loganwright/Dictionary+KeyPaths.swift
Created January 3, 2016 02:14
Show Gist options
  • Select an option

  • Save bluwave/d5de4c5e5a533f2b7a4f to your computer and use it in GitHub Desktop.

Select an option

Save bluwave/d5de4c5e5a533f2b7a4f to your computer and use it in GitHub Desktop.
Dictionary+KeyPaths
import Foundation
extension Dictionary {
mutating public func setValue(val: AnyObject, forKeyPath keyPath: String) {
var keys = keyPath.componentsSeparatedByString(".")
guard let first = keys.first as? Key else { print("Unable to use string as key on type: \(Key.self)"); return }
keys.removeAtIndex(0)
if keys.isEmpty, let settable = val as? Value {
self[first] = settable
} else {
let rejoined = ".".join(keys)
var subdict: [NSObject : AnyObject] = [:]
if let sub = self[first] as? [NSObject : AnyObject] {
subdict = sub
}
subdict.setValue(val, forKeyPath: rejoined)
if let settable = subdict as? Value {
self[first] = settable
} else {
print("Unable to set value: \(subdict) to dictionary of type: \(self.dynamicType)")
}
}
}
public func valueForKeyPath<T>(keyPath: String) -> T? {
var keys = keyPath.componentsSeparatedByString(".")
guard let first = keys.first as? Key else { print("Unable to use string as key on type: \(Key.self)"); return nil }
guard let value = self[first] as? AnyObject else { return nil }
keys.removeAtIndex(0)
if !keys.isEmpty, let subDict = value as? [NSObject : AnyObject] {
let rejoined = ".".join(keys)
return subDict.valueForKeyPath(rejoined)
}
return value as? T
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment