Retrieving array data stored in a plist file
How to get array data stored in plist file.
Plist file and source code as below,
------------------------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>abcxxx</string>
<key>Phones</key>
<array/>
<key>item1</key>
<string>121327777</string>
<key>item2</key>
<string>333333333</string>
</dict>
</plist>
---------------------------------------------------------
-(id) init
{
NSString *errorDesc = nil;
NSPropertyListFormat format;
phoneNumbers =[[NSMutableArray alloc]init];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"testfile" ofType:@"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription:&errorDesc];
if(!temp){
NSLog(errorDesc);
[errorDesc release];
}
self.personName = [temp objectForKey:@"Name"];
self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]];
NSLog(@"Label executed %@",[phoneNumbers objectAtIndex:0]);
UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(-180, 100, 100, 20)];
name.text = [phoneNumbers objectAtIndex:1];
[self addChild:name];
NSLog(@"Label executed %@",name);
0
bhanu
a source
to share
4 answers
Your XML plist file looks a little fishy. The tag <array/>
you have is a separate tag, which I am not sure if I will do anything useful.
Maybe change the plist to something 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>Name</key>
<string>abcxxx</string>
<key>Phones</key>
<array>
<string>121327777</string>
<string>333333333</string>
</array>
</dict>
</plist>
I'm not sure if this will help with your code. Have you read this documentation?
+1
a source to share
All you have to do is call the NSDictionary initWithContentsOfFile: method. for instance
NSDictionary *temp = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.personName = [temp objectForKey:@"Name"];
self.phoneNumbers = [[temp objectForKey:@"Phones"] mutableCopy];
However, as mentioned above, your plist looks a little fishy, try changing it to the one suggested by Andy White.
Hello,
Kyle
0
a source to share
Most likely you just won't be able to read the plist, just check your plist structure,
NSString *StringsFromPList = [[NSBundle mainBundle] bundlePath];
NSString *itemPositionPlistLocation = [StringsFromPList stringByAppendingPathComponent:@"Words.plist"];
_myDictionary= [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
NSArray * items = [_myDictionary objectForKey:@"Root"];
<plist version="1.0">
<dict>
<key>Root</key>
<array>
<string>Happy</string>
<string>Rain</string>
</array>
Hope this helps :)
0
a source to share