Archive for October, 2010

No I didn’t bought a Windows Phone 7 but now it sure is in my wish list 😀 I spent this weekend developing an app for windows phone 7 and thanks to previous knowledge of silverlight and c#, it all seemed too easy 😀 A couple of weeks more and then I can add yet another platform on my Resume 😀

NB: This was my first post from the WordPress iPhone App, yay :-)…

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;