Using PLIST: Part 1 – Reading PLIST Files

In many applications you will have a need to store information in key/value pairs in a way that will persist between application sessions. One effective way to do this is with plists.

What is a plist?

Very simply, a plist file is an XML file that stores data in key/value relationship. In practice it’s very much like a dictionary in Swift. Every iOS project has at least one property list: Info.plist. This file is required by Xcode to build and run your application. Custom property lists can be a good place to store information that your app needs to function, but that doesn’t change very often.

When should you use a plist?

Property lists are idea for storing information that doesn’t need to change often but is still mutable. For example you might want to store the URLs for your app’s web services in a property list or localized string values for your user interface.

How to create a PLIST

Before we can read a plist from the file system, we need to create a list and add some values so they can be read and parsed. To create a plist, open the New File dialog in Xcode and in the filter box type “Property”. Under Resources, choose “Property List”, click next and then name and create your file. In this example I’ll name my file “AppSettings”

Now that we have an empty property list, we need to add some values to it. In this instance, let’s assume that the plist will store app configurations such as the web services url for our app’s services. To create new values, with the empty plist file displayed in your editor, hover your mouse over the Root row and click the plus button.

This will create a new entry to which you can now assign a key, data type and value. Use the drop-down Type menu to assign the appropriate type from the list of available types including (Array, Dictionary, Boolean, Data, Date, Number and String)

How to read a PLIST with Swift

In order for any data store to be useful, you have to have access to it from your code. Here’s how you can read the values from a plist:

func getPropertyList(name: String) -> NSDictionary {
        
	// First let's get the path to our property list
	guard let path = Bundle.main.path(forResource: name, ofType: "plist"),
		let xml = FileManager.default.contents(atPath: path) else {
	// If the property list doesn't exist, we'll throw an error for now.
		fatalError("Unable to load the requested property list")
    }
	// With data read into the xml variable we can now decode into a dictionary object.
	let propertyDictionary = try? PropertyListSerialization.propertyList(from: xml, options: .mutableContainersAndLeaves, format: nil) as? NSDictionary
	return propertyDictionary!
        
}

Now that we have a function to read the settings property list, we can call this function in when the user launches the app and store this data in a dictionary for use elsewhere in the app. We’ve kept this function generic so that it can be reused to load any property list file, by passing in the file name.

For the sake of simplicity in this tutorial, we’re going to store the resulting appSettings as a variable in the app delegate. So at the top of AppDelegate.swift, we’ll define an NSDictionary to store the data.

var appSettings: NSDictionary!

Now we can define a function that takes care of loading the app’s settings and call it when the app launches. First define the loadAppConfigs() function.

func loadAppConfigs() {
    appSettings = getPropertyList(name: "AppSettings")
}

Right now this function is pretty simple, but it’s a good place in which to do additional setup work such as choosing which key values to use or constructing an object to store these values (remember, storing values in AppDelegate isn’t ideal).

Finally, we’ll call this at the end of the didFinishLaunchingWithOptions delegate method.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    loadAppConfigs()
    return true
}

In Parts 2 and 3 of this series we’ll talk about how to read plist using Codable and how to write plists from within your code.

Leave a Reply