Here is my problem. I have a plist called myPlist and it looks like this:
<?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>1</key>
<array>
<string>Dog</string>
<string> empty </string>
<true/>
</array>
<key>2</key>
<array>
<string>Car</string>
<string> empty </string>
<false/>
</array>
<key>3</key>
<array>
<string>Table</string>
<string> empty </string>
<true/>
</array>
</dict>
It has three values for each key. Two are strings and one is Boolean. I tried the ‘usual ways’ to fill an nsmutablearray with the plist so I could show it in a tableview but the NSMutableArray is always empty. I tried this first:
NSMutableArray *mutable = [[NSArray arrayWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"]] retain];
Then I also tried this but it still returns 0 objects.
NSString *path = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *mutable = [NSArray arrayWithArray:[dict objectForKey:@"Root"]];
I always used dictionary-like plist and this is my first one with this kind of structure so I think I’m doing something wrong. Btw did I mention that I’m a noob? 🙂
Please help.
EDIT
Thanks to Rob, I can now read data using this line:
NSDictionary *dogObject = [dict objectForKey:@"1"];
But how can I transfer the data to a NSMutableArray so I could edit the data later? Or can I just save the edited data using dictionary?
You are missing the end-tag for the top level
plisttag. I assume that is a copy/paste error.If you add the missing
</plist>at the end, that is a valid property list. It contains a top-level dictionary, so you have to load it into anNSDictionary, not anNSArray(orNSMutableArray).The top-level dictionary doesn’t contain the key
Root. It contains three keys:1,2, and3. So you could get an object from it like this: