Skip to content

Instantly share code, notes, and snippets.

@Lancewer
Created January 26, 2018 00:54
Show Gist options
  • Save Lancewer/54f49a1d29adc916f992dd6641762c10 to your computer and use it in GitHub Desktop.
Save Lancewer/54f49a1d29adc916f992dd6641762c10 to your computer and use it in GitHub Desktop.
[ConvertPlistToDictionary] new API that replace NSDictionary.init?(contentsOfFile path: String) #Dictionary #plist #contentOfFile

Since NSDictionary.init?(contentsOfFile path: String) is deprecated from iOS 11, so this is a simple demo shows how to replace it with new APIs.

class func getCategories() -> (categoryDict:[String:Any], level1CateArray:[String]) {
var categoriesDict:[String:Any] = [:]
var level1Categories:[String] = []
if let path = Bundle.main.path(forResource: "Category", ofType: "plist") {
let rawData = try! Data(contentsOf: URL(fileURLWithPath: path))
if let categoryDict = try! PropertyListSerialization.propertyList(from: rawData, format: nil) as? [String:Any] {
categoriesDict = categoryDict
for k in categoryDict.keys {
level1Categories.append(k)
}
}
}
return (categoriesDict, level1Categories)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment