Load data from property list

Property list offers a convenient way to store simple structural data. It usually appears in XML format. You can’t use property lists to save all types of data. The items of data in a property list are of a limited number of types including arrays,dictionaries, strings, etc. In general. It is always better to separate static data from the code, especially when you have lots of items. Using property lists it is easier to change items, less error-prone and you can even delegate the maintenance to other people. In this tutorial we will create a table, which imports the data form the property list.

Open Xcode and create a new Single View Application. For product name, use PropertyListDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards is deselected and Use Automatic Reference Counting checkboxes is selected (Unit Tests will not be necessary for this project).

First, create The User Interface. Select the ViewController.xib and drag a Table View to the View.

Build and Run, the Table view is currently empty as we haven't provided the data yet.

First we need to set the delegate methods conforming to the UITableViewDelegate and UITableViewDataSource protocols. In ViewController.m change the @interface line in

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

In Interface Builder make the following connections

  • Table View -> delegate
  • Table View -> datasource

Create a new file, go to the resource section and select property list.  Name the property list TableData.plist

Open the property list and add an Array(AppleDevice) containing multiple Strings.

When you select the TableData.plist file, and open it as source code , you can view the created xml code

<?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">
<string>iPod touch</string>

Next, in viewDidLoad we load the items in our property file

// Find out the path of recipes.plist 
NSString *path = [[NSBundle mainBundle] pathForResource:@"TableData" ofType:@"plist"];

NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
tableData = [dictionary objectForKey:@"AppleDevice"]

Before reading the “tableData.plist” file, you have to first retrieve the full path of the resource. Then the key-value pair of the dictionary will be read.

Implement the delegate methods in ViewController.m


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
return [tableData count];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellIdentifier = @"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

return cell;

Build and Run, the table is populated with the data of the property list.

You can download the source code of the PropertyListDemo at the ioscreator repository on github.