iOS Property List (plist) Example

Filed Under: iOS

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.


import UIKit

class ViewController: UITableViewController {

    
    @IBOutlet var myTable: UITableView!
    

    let textCellIdentifier = "cell"
    var tableData = [String]()
    var tableValues = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        myTable.delegate = self
        myTable.dataSource = self
        
        let path = NSBundle.mainBundle().pathForResource("MyList", ofType: "plist")
        let dict = NSDictionary(contentsOfFile: path!)
        
        tableData = dict!.objectForKey("Version Code") as! [String]
        tableValues = dict!.objectForKey("OSX") as! [String]
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if  segue.identifier == "mySegue"
        {
            let s1 = segue.destinationViewController as! SecondView
            
            let blogIndex = tableView.indexPathForSelectedRow?.row
            
            s1.pass = tableValues[blogIndex!]
            
            
        }
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableData.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell
        
        let row = indexPath.row
        cell.textLabel?.text = tableData[row]
        
        return cell
    }
    

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
    }


}


  • 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.


import UIKit

class SecondView: UIViewController {

    
    var pass:String?
    
    @IBOutlet var label: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()

        if let name = pass {
            
            label.text=name
        }
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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.

Comments

  1. jyoti says:

    i want to read the data in array which is in dictionary form. how to read the data

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages