当前位置: 动力学知识库 > 问答 > 编程问答 >

objective c - How come reading from a plist fails in my iOS application?

问题描述:

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?

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: