在我的iOS应用程序中如何从plist中读取失败?

时间:2012-04-03 00:52:07

标签: objective-c ios

我正在尝试使用此功能在我的iOS应用中创建一个包含动物列表的NSMutableDictionary

// 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.
}

我将myAnimals.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>
    <string>elephant</string>
    <string>monkey</string>
    <string>cat</string>
</array>
</plist>

然而,当我到达断点(如我的评论中所列)时,我看到myAnimalDictionary为零。为什么呢?

1 个答案:

答案 0 :(得分:3)

也许是因为你的.plist中有一个数组,但你正试图实例化一个可变的字典实例。

相关问题