Working with Property Lists in Xcode 4.2


By

Property lists are designed specifically for Objective-C apps to store and retain data. These are straight XML files formatted for easy access to bundles and application resources. There isn’t anything overly confusing about Property Lists, but they are unconventional compared to web applications.

In this guide I’d like to go over property lists in a bit of detail in relation to Xcode. Both Mac OS X and iOS app developers will find these property lists very handy in aspects of programming. They are also perfect for storing types of object data, such as personal user characteristics(birthdays, friends, e-mail address, etc.).

Brief Introduction

Property lists are also known as plists because of their unique file extension. If you create a new Xcode project and open the “Supporting Files” group you should find a .plist info file with the same name as your project. This stores general app information such as the icon and bundle identifier.

Let’s first create a new property list that we can play around with. Right click on the Supporting Files group and select New File…

Xcode 4.2 screen - new file template from iOS property list

This brings up a dialog box with dozens of options. Regardless of iOS or Mac OS X select the “Resource” section and choose Property List from the available options. Give it a name and Xcode will automatically create this new file and save it into your application folder. Now we have a great place to start!

Adding Some Data

You’ll recognize this is a totally blank file that has been created. So let’s get started adding a few rows into our document. If you double-click on the .plist file in your Project Navigator(left-hand column files viewer) this will open it into a new window. I find this is a bit easier to work with since the Xcode app can take up a lot of screen real estate.

Now simply right-click or control+click anywhere in the blank document and select Add Row. This creates our first XML entry with the key “New item”. Each item in our document is based upon a key => value pairing. This first row we’ve just created is known as our root item.

But you should notice that each row is also setup with a specific data type. There are two core types – containers and static row data. Containers are split between Dictionaries and Arrays and will always contain child rows(the plist document root holds either dictionaries or arrays).

plist example from tab bar navigation data

Static data is exactly what it sounds like: a row with a set value of information. Typically you would create a set of arrays or dictionaries to hold these different static bits of data. Below are the 5 different types you can choose from:

  • Boolean
  • Data
  • Date
  • Number
  • String

Dictionaries vs. Arrays

This interesting idea has popped up on more than one occasion. By default Xcode 4 will create your property lists running with new dictionary items. The difference between dicts and arrays is that each dictionary row is given a unique key name.

Arrays just get the objects numerically so you can loop through them in code. It’s annoying when you have to change between these two types of containers since Xcode doesn’t offer an easy solution. But now let’s change our “New item” into an array so our document doesn’t rely on Keys.

Editing the Source Code

Close the new opened window and move back into the main Xcode interface. Right-click or control+click on our plist and select Open As -> Source Code. This allows us to directly edit the XML content which is currently the only way to change from the default dictionary listing.

Xcode and Objective-C programming Mac OSX

Look at the structure we have setup currently and you’ll see a tag wrapped <dict></dict>. We need to change these to read array similar to what I have below.

<plist version="1.0">
<array>
	<key>New item</key>
	<integer>0</integer>
</array>
</plist>

If we switch back to Property List view you’ll see our row is now indexed as “Item 0”. Now if we go to add in new rows they will automatically fill in the key name incrementing by +1 each time. This is my preferred method of setting up a Property List document since dictionary keys can get very confusing very quickly.

Building a Small Demo

Now to put this all together I want to build a simple demo we can use for future reference. I’ve created a document named SpeckyPosts.plist which will hold an array of 5 different dictionaries. Inside each dict I’ve added 3 static rows of data – a blog post title, URL, and number of comments.

This is confusing to explain in words but it makes a lot of sense once you look at the code. Basically our root document contains an array with five items, and these items are typecast as dictionaries. Inside each dictionary we have 3 key => value pairs which are postTitle, URL, and comments. You can also copy/paste any of the array items to duplicate content and add more posts into the list.

SpeckyApp Info plist data view

This setup is very easy to format and loop through in your code. If you’d like to take a peek at my example document you can download it here or go through the source code as direct XML.

<plist version="1.0">
<array>
	<dict>
		<key>postTitle</key>
		<string>On Tastes, Trends and Personal Styles</string>
		<key>URL</key>
		<string>https://speckyboy.com/2012/03/18/on-tastes-trends-and-personal-styles/</string>
		<key>comments</key>
		<integer>0</integer>
	</dict>
	<dict>
		<key>postTitle</key>
		<string>Say Hi To Anchor – A Super-Tiny CMS</string>
		<key>URL</key>
		<string>https://speckyboy.com/2012/03/09/say-hi-to-anchor-a-super-tiny-cms/</string>
		<key>comments</key>
		<integer>14</integer>
	</dict>
	<dict>
		<key>postTitle</key>
		<string>A Showcase of Enchanting Vintage Advertisements</string>
		<key>URL</key>
		<string>https://speckyboy.com/2012/03/04/a-showcase-of-enchanting-vintage-advertisements/</string>
		<key>comments</key>
		<integer>6</integer>
	</dict>
	<dict>
		<key>postTitle</key>
		<string>The Challenge of Responsive Images</string>
		<key>URL</key>
		<string>https://speckyboy.com/2012/03/05/the-challenge-of-responsive-images/</string>
		<key>comments</key>
		<integer>5</integer>
	</dict>
	<dict>
		<key>postTitle</key>
		<string>How to Build a Sliding One Page Portfolio with jQuery</string>
		<key>URL</key>
		<string>https://speckyboy.com/2011/10/12/how-to-build-a-sliding-one-page-portfolio-with-jquery/</string>
		<key>comments</key>
		<integer>27</integer>
	</dict>
</array>
</plist>

Useful Resources

If you’d like to explore plists further on your own then check out some of the links below. There are tons of tutorials & websites you can browse, and even web forums where developers are happy to answer some of your more detailed questions.

Conclusion

I hope this introduction to plists has peaked your interest in Xcode and app development altogether. Apple’s NeXTSTEP library is full of unique code examples just like this. It takes a bit of practice getting used to these methodologies at first. But following standards will streamline your development process and make debugging a whole lot easier.

Related Topics


Top
This page may contain affiliate links. At no extra cost to you, we may earn a commission from any purchase via the links on our site. You can read our Disclosure Policy at any time.