Skip to content

Instantly share code, notes, and snippets.

@IanKeen
Created April 4, 2016 19:27
Show Gist options
  • Save IanKeen/43075da66b11a6c52fbc818270e34285 to your computer and use it in GitHub Desktop.
Save IanKeen/43075da66b11a6c52fbc818270e34285 to your computer and use it in GitHub Desktop.
Reading plists into a typed Dictionary: Snippet 5
extension Dictionary {
init(plistNamed fileName: String, bundle: NSBundle = NSBundle.mainBundle()) {
guard
let pList = bundle.pathForResource(fileName, ofType: "plist"),
let dict = NSDictionary(contentsOfFile: pList)
else { self = [:]; return }
var result = [Key: Value]()
dict.forEach { key, value in
if let key = key as? Key, let value = value as? Value {
result[key] = value
}
}
self = result
}
}
//Usage:
let data = Dictionary<String, Int>(plistNamed: "Info")
/* or */
let data: [String: Int] = Dictionary(plistNamed: "Info")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment