Chapter 11. Application Settings

A setting, or preference, for an application contains two pieces: a key and a value. The key refers to a unique value identifying the setting, for example musicVolume. The value refers to the value stored for the given key. Working with settings involves creating and modifying key/value pairs within property lists.

Property lists are XML-formatted files that store the key and value pairs that make up an application’s settings. They are nothing new to Apple developers, as they are supported by most recent versions of Mac OS X, and can be read and written to directly from dictionary classes without actually having to parse a file.

Dictionaries and Property Lists

Property lists are flat files on disk, but are represented in your application as a dictionary with keys and values. The NSDictionary class provides the methods needed to read and write property lists to and from disk, and can realize key and value pairs without implementing any complex parsing functions by the developer. The NSMutableDictionary class builds on top of this to provide a mechanism to add and remove individual key/value pairs.

Creating a Dictionary

Dictionaries can be created in a number of ways. The simplest way is to use the NSMutableDictionary class:

NSMutableDictionary *dict = [ [ NSMutableDictionary alloc ] init ];

Managing Keys

Once you have created the dictionary, you can add individual key/value pairs to it using the setValue method:

[ dict setValue: @"myValue" forKey: @"myKey" ...

Get iPhone SDK Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.