Created
April 4, 2016 19:27
-
-
Save IanKeen/43075da66b11a6c52fbc818270e34285 to your computer and use it in GitHub Desktop.
Reading plists into a typed Dictionary: Snippet 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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