Reputation: 1161
How can I get the content of this plist?
I am a little confused on the different elements and how to access them.
How does one access these dictionaries, and dictionaries in dictionaries?
What if I want to copy this data first, would it increase performance by a lot, rather than always reading from the plist?
How can I get all of this data into my app, by going through the structure levels?
Thank you
Upvotes: 2
Views: 3666
Reputation: 1534
let filePath:String = NSBundle.mainBundle().pathForResource("infoList_English", ofType: "plist")!
let postDictionary : NSDictionary = NSDictionary(contentsOfFile: filePath)!
print(postDictionary)
Upvotes: 0
Reputation:
use this :
NSDictionary *dic1 = [[NSDictionary alloc] initWithContentofFile:yourFilePathhere];
// return main dictionary
NSDictionary *dic2 = [dic1 objectForKey:mainDictionary];
Upvotes: 1
Reputation: 36762
The easiest way to read a property list is by using a convenience method on NSDictionary
(Or NSArray
if your root element is an array) like so:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"FileName"
ofType:@"plist"];
NSDictionary* plist = [NSDictionary dictionaryWithContentsOfFile:filePath];
From there on the plist file is just a normal dictionary. Use objectForKey:
, etc to get to the values. For faster access to deep child nodes you can use valueForKeyPath:
like this for example:
NSString* name = [plist valueForKeyPath:@"mainDictionary.name"];
For more complex operations on plist files you should use NSPropertyListSerialization
that have several class methods for more more fine grained access to reading and writing property list files.
Upvotes: 10
Reputation: 2382
You should check out the NSPropertyListSerialization class, which makes parsing plists relatively straightforward.
Here's a bit of sample code: http://iphoneincubator.com/blog/tag/nspropertylistserialization
Upvotes: 0