我正在尝试使用此功能在我的 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 为 nil。为什么?
最佳答案
可能是因为您的 .plist
中有一个数组,但您正试图实例化一个可变字典实例。
关于objective-c - 为什么在我的 iOS 应用程序中从 plist 读取失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9985782/