How to read data from plist(Property List) in iOS Swift.

Shubham Kaliyar
Jan 30 · 3 min read
Image for post
Image for post

hey guys ,

What’s A Plist?

A property list, or plist, is an XML file that contains key-value data. It’s easiest to compare with a dictionary in Swift, so it’s a list of values associated with keys. Here’s an example:

<?xml version=”1.0" encoding=”UTF-8"?> <!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version=”1.0"> <array> <string>Orange</string> <string>Pineapple</string> <string>Raspberry</string> <string>Banana</string> <string>Pear</string> <string>Apple</string> </array> </plist>

In Xcode you can see a plist’s XML structure by right-clicking on a .plist file, and choosing Open As -> Source Code.

If you look closely, you can spot the different values and structures in the XML. Here’s a brief overview from top to bottom:

  • The <?xml>, <!DOCTYPE> and <plist> lines are boilerplate code that give information about the XML itself. A typical XML file has a so-called DTD, which contains information about the structure of XML.
  • The top level <array> tag wraps the array items in it. Every XML element has an opening, with the <tagname> format, and closing tag, with the </tagname> format.
  • Inside the array you see 6 <string> tags. The array contains a few fruit names as strings.

Reading Data from Plist in Swift..

guard let path = Bundle.main.path(forResource: “CallingCodes”, ofType: “plist”) else {return}

let url = URL(fileURLWithPath: path)

let data = try! Data(contentsOf: url)

guard let plist = try! PropertyListSerialization.propertyList(from: data, options: .mutableContainers, format: nil) as? [[String:String]] else {return}

print(plist)

Writing Data To A Plist

Can you also write a dictionary or array back to a property list file? Of course! Let’s say that the user doesn’t have a Preferences property list yet, so we’ll create a default for them. Here’s how:

let preferences = Preferences(webserviceURL: "https://medium..com", itemsPerPage: 10, backupEnabled: false)

With the above code we’re creating a new instance of Preferences that’s not yet stored as a property list. And here’s how we save it:

let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("Preferences.plist")do {
let data = try encoder.encode(preferences)
try data.write(to: path)
} catch {
print(error)
}

So we can use plist file to store data and read data . plist is light weight , so please avoid json file to store data (CountryCodepicker data ), use plist for this …..
Good Day

NSNotification Center in iOS Swift

https://medium.com/@shubhamkaliyar255/nsnotificationcenter-in-ios-swift-1dc00c686d42

Shubham Kaliyar

Written by

iOS Developer with over 2 years of experience in the field with great passion for technology. Software is like sex: It’s better when it’s free..

Shubham Kaliyar

Written by

iOS Developer with over 2 years of experience in the field with great passion for technology. Software is like sex: It’s better when it’s free..

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store