Skip to content

Instantly share code, notes, and snippets.

@coryalder
Created February 21, 2015 06:05
Show Gist options
  • Save coryalder/2d3055e58fa79c35f302 to your computer and use it in GitHub Desktop.
Save coryalder/2d3055e58fa79c35f302 to your computer and use it in GitHub Desktop.
func traverse(keyPath: [String], dict: Dictionary<String, AnyObject>?) -> AnyObject? {
if dict == nil {
return nil
}
var paths = keyPath
let next = paths.first!
if paths.count > 1 {
paths.removeAtIndex(0)
if let nextDict = dict![next] as? Dictionary<String, AnyObject> {
return traverse(paths, nextDict)
} else { // fail because bad path
return nil
}
} else if paths.count == 1 {
return dict![next]
} else {
println("No PATHS in traverse, returning nil")
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment