-
-
Save shahdhiren/d6c3abf4d68533d078d9040c221c2704 to your computer and use it in GitHub Desktop.
Swift 4 recipes: Extract data from a property list
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
//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