在Cocoa阅读plist文件
问题描述:
我有一个plist,看起来如下。什么是读入plist的首选方法,以及如何将它读入数组?我可以创建一个NSMutableDictionary
,加载这个plist的内容,将这些键加载到一个Array中,然后使用for循环来读取前两个键 - SomeData
和MoreData
。但我无法解析所有其他密钥(Key1
,Key2
和Key3
)。我会如何去做这件事?粘贴的代码在下面通过顶部2键。 (SomeData
和MoreData
)。谢谢。在Cocoa阅读plist文件
的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">
<dict>
<key>SomeData</key>
<array>
<dict>
<key>Key1</key>
<false/>
<key>Key2</key>
<dict>
<key>Key3</key>
<integer>0</integer>
</dict>
</dict>
</array>
<key> MoreData</key>
</dict>
</plist>
代码:
-(IBAction)modifyPlist:(id)sender{
NSLog(@"Listing Keys");
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:@"/Users/sureshb/Documents/myprogs/plistMod/com.test.plist"];
NSArray *keys = [dict allKeys];
for (NSString *key in keys){
NSLog(@"%@",key);
}
}
答
如果我没有记错,你可以在明年的方式访问这些值:
NSArray *someData = [dict objectForKey:@"SomeData"];
BOOL key1 = [[[someData objectAtIndex:0] valueForKey:@"Key1"] boolValue];
int key2 = [[[[someData objectAtIndex:0] objectForKey:@"Key2"] objectForKey:@"Key3"] intValue];
感谢Nekto。这工作。 – cocoacoder
不客气 – Nekto