scottD
scottD

Reputation: 135

Trouble Loading Data From Plist Into Array

I have a strange problem that probably has a simple solution that I just can't find/figure out. Any ideas are greatly appreciated.

I have a property list that consists of an array as the root object. The array contains a couple dictionaries.

The code below shows how I'm attempting to load the plist data into my array-

-(void)awakeFromNib{

    NSString *plistPath = @"/Users/administrator/Desktop/WipeOut Winner!/Data.plist";
    NSLog(@"Plist path is %@",plistPath);


    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
            NSLog(@"File wasn't found");
    }


    tableDataArray = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];

    NSLog(@"array count is %lu",[tableDataArray count]);   

}

And below is the 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>root</key>
    <array>
        <dict>
            <key>winner</key>
            <string>Scott</string>
            <key>personChosen</key>
            <string>Female</string>
            <key>winType</key>
            <string>2 and &apos;Won&apos; -3 pts.</string>
            <key>Points</key>
            <string>3</string>
        </dict>
        <dict>
            <key>winner</key>
            <string>Emily</string>
            <key>personChosen</key>
            <string>Male</string>
            <key>winType</key>
            <string>All The Way! -5 pts.</string>
            <key>points</key>
            <string>5</string>
        </dict>
    </array>
</dict>
</plist>

The plist file is being found as it should be. And I have confirmed that my root object in the plist is an array (of dictionaries). My array count after this call always turns up zero though.

During the process of trying to figure it out, I changed the data in my plist to a single NSDictionary instead of an array containing dictionaries. Then I loaded the plist into an NSDictionary, loaded that dictionary into the array, and the array count was 1 as expected, so I know for sure that things are hooked up correctly as far as finding, loading, etc. of the plist goes. In another test, rather than trying to load the array with the plist data like below

tableDataArray = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];

I bypassed the plist stuff all together and just loaded the array like so-

tableDataArray =[[NSMutableArray alloc]initWithCapacity:1];

NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:@"Emily",@"name",@"12",@"age", nil];

[tableDataArray addObject:dict];

and that also changed the array count to one as expected. So I'm thinking my problem has to be something to do with this

tableDataArray = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];

but I can't seem to figure out where.

Upvotes: 0

Views: 416

Answers (1)

Sebastian Celis
Sebastian Celis

Reputation: 12195

The root of your plist isn't an array. It is a dictionary with a single key "root" that points to an array of dictionaries. Your plist should instead look like:

<?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">
<array>
    <dict>
        <key>winner</key>
        <string>Scott</string>
        <key>personChosen</key>
        <string>Female</string>
        <key>winType</key>
        <string>2 and &apos;Won&apos; -3 pts.</string>
        <key>Points</key>
        <string>3</string>
    </dict>
    <dict>
        <key>winner</key>
        <string>Emily</string>
        <key>personChosen</key>
        <string>Male</string>
        <key>winType</key>
        <string>All The Way! -5 pts.</string>
        <key>points</key>
        <string>5</string>
    </dict>
</array>
</plist>

Upvotes: 1

Related Questions