duck1970
duck1970

Reputation: 137

Load NSMutableDictionary from plist

I'm trying to load an NSMutableDictionary from a Plist. My result is always NULL.

Plist

<?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>LocNum</key>
            <string>0</string>
            <key>LocName</key>
            <string>Head</string>
        </dict>
        <dict>
            <key>LocNum</key>
            <string>1</string>
            <key>LocName</key>
            <string>Left Hand</string>
        </dict>
        <dict>
            <key>LocNum</key>
            <string>2</string>
            <key>LocName</key>
            <string>Right Hand</string>
        </dict>
       ....
    </array>
</plist>

My code is pretty simple:

locDictionary = [[NSMutableDictionary alloc] init];
NSString *locListpath = [[NSBundle mainBundle] pathForResource:@"LocList" ofType:@"plist"];
locDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:locListpath];
NSLog(@"locDictionary %@", locDictionary);

Any thoughts or help? Thanks

Upvotes: 0

Views: 581

Answers (2)

michaelrccurtis
michaelrccurtis

Reputation: 1172

Your plist defines an array of dictionaries, not a dictionary itself, so you need to initiate an array:

NSArray *array = [[NSArray alloc] initWithContentsOfFile:locListpath];

and then loop through the array to get the individual dictionaries with something like:

for (NSDictionary *dict in array) {
...
}

Upvotes: 1

teamnorge
teamnorge

Reputation: 794

Your plist is not NSDictionary, but NSArray of dictionaries, if it is placed in proper place on disk then this code should help:

NSString *path = [[NSBundle mainBundle] pathForResource:@"LocList" ofType:@"plist"];
NSMutableArray *locArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

Upvotes: 2

Related Questions