Skip to content

Instantly share code, notes, and snippets.

@chuthan20
Created March 10, 2016 03:36
Show Gist options
  • Save chuthan20/3015b3796430b4b374f5 to your computer and use it in GitHub Desktop.
Save chuthan20/3015b3796430b4b374f5 to your computer and use it in GitHub Desktop.
Similar to key-path way to access values in swift dictionary
import Foundation
extension Dictionary {
func JSONValue(path:String) -> AnyObject? {
let dict = self as! NSDictionary
return dict.JSONValue(path)
}
}
extension NSDictionary {
private func arrayIndex(comp:String) -> (String, String?)? {
let opn = NSStringCompareOptions.BackwardsSearch
let rng = comp.fullRange
guard let start = comp.rangeOfString("[", options: opn, range: rng),
end = comp.rangeOfString("]", options: opn, range: rng) else {
return nil
}
let name = comp.substringToIndex(start.startIndex)
let index = comp.substringWithRange(start.startIndex.advancedBy(1) ..< end.endIndex.advancedBy(-1))
return (name, index)
}
func JSONArray(dict:[String:AnyObject], name:String, index:String?) -> AnyObject? {
if let ind = Int(index!) {
return (dict[name] as? [AnyObject])?[ind]
}
return nil
}
func JSONValue(path:String) -> AnyObject? {
var dict:AnyObject = self
var components = path.componentsSeparatedByString(".")
guard let last = components.last else { return nil }
if components.count >= 1 {
components = Array(components.dropLast(1))
}
for comps in components {
if let arrIndex = arrayIndex(comps) {
dict = JSONArray(dict as! [String : AnyObject], name:arrIndex.0, index: arrIndex.1)!
} else {
if let dict1 = dict[comps] as? [String:AnyObject] {
dict = dict1
}
}
}
if let arrIndex = arrayIndex(last) {
return JSONArray(dict as! [String : AnyObject], name:arrIndex.0, index: arrIndex.1)!
} else {
return (dict as? [String:AnyObject])?[last]
}
}
}
extension String {
var fullRange: Range<String.Index> {
return self.startIndex ..< self.endIndex
}
}
// usage + test
let testData = [ "one":
[ "two":
[
"four":4,
"three":["hello", "how", "are"]]
],
"boo": [1, 2, "bosob", 4],
"arr_obj": [ ["one":1, "two":2], ["two":2, "three":3], ["four":4, "three":3] ]
]
testData.JSONValue("arr_obj[0].one") // 1
testData.JSONValue("arr_obj[0].one") // 1
testData.JSONValue("arr_obj[0].two") // 2
testData.JSONValue("arr_obj[1].two") // 2
testData.JSONValue("arr_obj[1].three") // 3
testData.JSONValue("arr_obj[2].three") // 3
testData.JSONValue("arr_obj[2].four") // 4
testData.JSONValue("one")
// {
//two = {
// four = 4;
// three = (
// hello,
// how,
// are
// );
//};
//}
testData.JSONValue("one.two")
// {
//four = 4;
//three = (
// hello,
// how,
// are
//);
//}
testData.JSONValue("one.two.four") // 4
testData.JSONValue("one.two.three")
// (
//hello,
//how,
//are
//)
testData.JSONValue("one.two.three[0]") // hello
testData.JSONValue("one.two.three[1]") // how
testData.JSONValue("one.two.three[2]") // are
testData.JSONValue("boo")
// (
// 1,
// 2,
// bosob,
// 4
//)
testData.JSONValue("boo[0]") // 1
testData.JSONValue("boo[1]") // 2
testData.JSONValue("boo[2]") // bosob
testData.JSONValue("boo[3]") // 4
testData.JSONValue("") // nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment