Reading plist files

Posted: October 15, 2010 in Snippets

It was only recently that I read about this function,

dictionaryWithContentsOfFile:

Creates and returns a dictionary using the keys and values found in a file specified by a given path.

+ (id)dictionaryWithContentsOfFile:(NSString *)path

Parameters

path

A full or relative pathname. The file identified by path must contain a string representation of a property list whose root object is a dictionary. The dictionary must contain only property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary). For more details, see Property List Programming Guide.

Return Value

A new dictionary that contains the dictionary at path, or nil if there is a file error or if the contents of the file are an invalid representation of a dictionary.

Availability

  • Available in iPhone OS 2.0 and later.

 

Hence, iPhone SDK has direct support for reading in plist files into a dictionary or an array.

Example:

NSString *propertiesPath = [[NSBundle mainBundle] pathForResource:@”properties” ofType:@”plist”];

NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:propertiesPath];

properties.plist file:

<?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”&gt;

<plist version=”1.0″>

<dict>

<key>Name</key>

<string>Basit</string>

<key>Array_item</key>

<array>

<string>1</string>

<string>2</string>

</array>

</dict>

</plist>

That would result in a dictionary with two key-value pairs

“Array_item” =     (

1,

2

);

Name = Basit;

 

Advertisements
Comments
  1. […] This cup of tea was served by: Basit Ali’s Blog […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s