Skip to content

Instantly share code, notes, and snippets.

@db42
Last active February 2, 2019 14:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save db42/e7a009dd864c7d40fd2a750e3ae24155 to your computer and use it in GitHub Desktop.
Save db42/e7a009dd864c7d40fd2a750e3ae24155 to your computer and use it in GitHub Desktop.
Swift 4 recipes: Extract data from a property list
//Property List file name = regions.plist
let pListFileURL = Bundle.main.url(forResource: "regions", withExtension: "plist", subdirectory: "")
if let pListPath = pListFileURL?.path,
let pListData = FileManager.default.contents(atPath: pListPath) {
do {
let pListObject = try PropertyListSerialization.propertyList(from: pListData, options:PropertyListSerialization.ReadOptions(), format:nil)
//Cast pListObject - If expected data type is Dictionary
guard let pListDict = pListObject as? Dictionary<String, AnyObject> else {
return
}
//Cast pListObject - If expected data type is Array of Dict
guard let pListArray = pListObject as? [Dictionary<String, AnyObject>] else {
return
}
//Perform actions on pListDict
} catch {
print("Error reading regions plist file: \(error)")
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment