views:

1084

answers:

3

Hi,

I have a problem, how can we retrieve the array data stored in plist file.

The plist file and source code is as shown 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"&gt;
<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);

Anyone help me on this.

Thanks in Advance,

+1  A: 

Your plist XML looks a little suspicious. The <array/> tag you have is just a standalone tag, which I'm not sure 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"&gt;
<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 whether this will help w/ your code. Have you read through this documentation?

http://developer.apple.com/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html

Andy White
A: 

Hi,

All you have to do is call NSDictionary's initWithContentsOfFile: method. For example

NSDictionary *temp = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.personName = [temp objectForKey:@"Name"];
self.phoneNumbers = [[temp objectForKey:@"Phones"] mutableCopy];

However, as mentioned above, your plist does look a little suspicious, try changing it to the one Andy White provided as well.

Regards,

Kyle

Kyle
That doesn't make all the containers and leaves mutable though, whereas the NSPropertyListSerialization technique does.
dreamlax
A: 

The frame of the UITextField is also weird (depending on self's frame) CGRectMake(-180, 100, 100, 20) is probably offscreen.

Roger Nolan