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:
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.
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:
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.
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.
Declare the relevant identifiers for the ViewControllers, the segue and the UITableViewCell. In the attributes inspector of the UILabel choose word wrap.
The ViewController.swift source code is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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.
This brings an end to this tutorial. You can download the iOS Property List Project from the below link.