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”>
<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;