I have a plist file in my bundle and I convert it to a dictionary using:
let path = Bundle.main.path(forResource: "myFile", ofType: "plist")
let dicFromFile = NSDictionary(contentsOfFile:path!)
myDictionary = dicFromFile as! [String : String]
But I also want the app to check for update of this file, so I put it on a sharing site, so it has some url, like so:
https://www.dropbox.com/s/8ri13i2wggaz6/myFile.plist
so, how do I make NSDictionary to read from this file's url?
To load data from a remote URL use an asynchronous way and the class PropertyListSerialization
to deserialize the property list.
let url = URL(string:"https://www.dropbox.com/s/8ri13i2wggaz6/myFile.plist")!
URLSession.shared.dataTask(with:url) { (data, response, error) in
if let error = error { print(error); return }
do {
let dictionary = try PropertyListSerialization.propertyList(from: data!, format: nil) as! [String:Any]
// do something with the dictionary
} catch {
print(error)
}
}.resume()
If the property list file contains only String
values – as assumed in the question – cast to [String:String]