如何从plist文件中获取字典数

时间:2012-05-31 13:56:33

标签: iphone

我是iphone的新手。我在从plist文件中检索数据时遇到了一些问题。实际上我的plist文件看起来像这样

键类型值 Genesis Dictionary(0件物品) Exodus Dictionary(0件物品) Leviticus Dictionary(0 items)

这里我如何检索plist文件中的字典数量,我希望输出为3。

如果有人知道这个请帮助我......

4 个答案:

答案 0 :(得分:1)

简单的回答,如果您真的接受了问题的答案,将不胜感激。

NSDictionary *dictionary = ....
int count = [[dictionary allKeys] count]; // Will return 3. allKeys will contain (Genesis,Exodus,Leviticus)

答案 1 :(得分:0)

你的plist返回字典或数组,然后为了知道plist包含多少个字典,你可以在字典对象上使用count消息。

答案 2 :(得分:0)

NSString *path = [[NSBundle mainBundle] pathForResource:@"yourResource" ofType:@"plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:path]) 
{
    NSLog(@"The file exists");
} 
else 
{
    NSLog(@"The file does not exist");
}

NSMutableDictionary *myDic = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
int count = [myDic count];
NSLog(@"%d",count);

答案 3 :(得分:0)

这个怎么样:

NSArray *myPlistPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES); 
NSString *documentsDirectory = [myPlistPath objectAtIndex:0]; 
NSString *myPath = [documentsDirectory
                         stringByAppendingPathComponent:@"MYPLIST.plist"]; 
NSDictionary *plistDictionary = [[NSDictionary alloc] initWithContentsOfFile:myPath]; 

NSArray *array = [[plistDictionary objectForKey:0]
                      sortedArrayUsingSelector:@selector(compare:)]; 

return [NSString stringWithFormat:@"%d MyPlist", [plistDictionary count]];