0

I am trying to create an NSMutableDictionary containing a list of animals in my iOS app with this function.

// puts animals into dictionary
- (void) putAnimalsFromPlistToDictionary
{
    NSBundle *bundle = [NSBundle mainBundle];    
    NSString *path = [bundle pathForResource:@"myAnimals" ofType:@"plist"];    
    myAnimalDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    // I set a breakpoint here in XCode.
}

I dragged myAnimals.plist (which looks like the below) into my supporting files folder for my application.

<?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>
    <string>elephant</string>
    <string>monkey</string>
    <string>cat</string>
</array>
</plist>

However, when I reach the breakpoint (as listed in my comment), I see that myAnimalDictionary is nil. Why?

Billy Goswell
  • 205
  • 1
  • 3
  • 11

1 Answers1

3

Perhaps it's because you have an array in your .plist but you're trying to instantiate a mutable dictionary instance.

Alexsander Akers
  • 15,967
  • 12
  • 58
  • 83
  • Thank you. Oh dear, that plist comes from the client. I can't change it. What can I make out of it then if I can't make a dictionary? Can I make a set? – Billy Goswell Apr 03 '12 at 01:17
  • Then why do you want a dictionary of the values? The data are listed as an array. – Alexsander Akers Apr 03 '12 at 01:21
  • I don't need a dictionary. I just need to somehow load a list of animals into any collection such as a set or an array. I think you're right actually - a dictionary implies key-value pairs, and I don't think I need that. – Billy Goswell Apr 03 '12 at 01:22
  • Then try `NSArray *animalArray = [[NSArray alloc] initWithContentsOfFile:path];` – Alexsander Akers Apr 03 '12 at 01:24