我试图从Plist加载NSMutableDictionary。我的结果总是为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>
我的代码非常简单:
locDictionary = [[NSMutableDictionary alloc] init];
NSString *locListpath = [[NSBundle mainBundle] pathForResource:@"LocList" ofType:@"plist"];
locDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:locListpath];
NSLog(@"locDictionary %@", locDictionary);
有任何想法或帮助吗?感谢
答案 0 :(得分:2)
你的plist不是NSDictionary
,而是NSArray
字典,如果它放在磁盘上的适当位置,那么这段代码应该有帮助:
NSString *path = [[NSBundle mainBundle] pathForResource:@"LocList" ofType:@"plist"];
NSMutableArray *locArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
答案 1 :(得分:1)
你的plist定义了一个字典数组,而不是字典本身,所以你需要启动一个数组:
NSArray *array = [[NSArray alloc] initWithContentsOfFile:locListpath];
然后循环遍历数组以获取各个字典,例如:
for (NSDictionary *dict in array) {
...
}