iOS Property List (plist) Example

There are many better ways to keep persistent data in our application rather than hardcoding the static texts. Three commonly used ways are NSUserDefaults, CoreData, plist. We’ll discuss and implement plist in this tutorial.

iOS Property List

Property List are used to store some data in a structural way that can be used on later. In raw form the property lists are in XML format. The data is stored in the form of a dictionary with key value pairs. Property lists can’t store all kinds of data. They are limited to certain types of data such as arrays, dictionaries, strings, Numbers etc. In this tutorial we’ll going to create our own property lists and save and use them in a table view application.

Create a new Project of the type Single View Application named PropertyLists. In the project view open the Info.plist. It should look like this:

ios-property-lists-default

Right click the Info.plist and select open as Source code. The Property lists will be seen in raw XML form.

Let’s create our own Property Lists. Create a new file. Choose iOS -> Resource -> Property List as shown below and give the desired name.

ios-property-lists-dialog

To add or delete rows press the respective buttons. We’ve created a property list that consists of two child arrays. One with Mac OSX Version Codes and the other with the Version Names.
Our Property lists file looks like this:

ios-property-lists-custom

We’ll create a TableViewController that’s embedded in a Navigation Controller. On pressing any tableview cell it takes us to a View Controller that displays a label text with the relevant Version Name of the cell pressed.

Project Structure

The project view consists of a ViewController.swift file (that’s subclassed to UITableViewController), a SecondViewController.swift file and the newly created property lists file.

ios-property-lists-project

Code

Delete the ViewController from the storyboard and add a new TableViewController. Embed it in a Navigation Controller and make the Navigation Controller the initial view controller. Add a new View Controller with a label text. Drag a segue from the UITableViewCell to this ViewController. The Storyboard should look like the one given below.

ios-property-lists-storyboard

Declare the relevant identifiers for the ViewControllers, the segue and the UITableViewCell. In the attributes inspector of the UILabel choose word wrap.

ios-property-lists-label-attributes

The ViewController.swift source code is given below.

  • We’ve changed the subclass type to UITableViewController and implemented all the methods conforming to the protocol.
  • The tableview object delegate and datasource are initialised to the ViewController object self.
  • The tableData and tableValues are initialised with the two arrays defined in the property lists file.
  • The segue object is passed with the item array of the version name using the index of the UITableViewCell clicked.

The SecondViewController.swift is defined below.

The output of the application in action is given below.

ios-property-lists-output

This brings an end to this tutorial. You can download the iOS Property List Project from the below link.

By admin

Leave a Reply

%d bloggers like this: