如何获得NSMutableDictionary计数在iPhone?

我想在iphone中获取NSMutableDictionary数。 我想知道NSMutableDictionry中有多less项。 我试过这些代码来找出解决scheme,但没有帮助我很多。

NSLog(@"Count : %d", [mutableDictionary count]); 

总是返回“0”。 如何获得iPhone中的NSMutableDictionary的计数? 提前致谢。

您可以找出有多less个键对象(键值)对:

 NSArray * allKeys = [mutableDictionary allKeys]; NSLog(@"Count : %d", [allKeys count]); 

编辑

通过查看字典文档, NSDictionarycount方法(或属性)也应该工作。 我想你可能已经收到了0,因为字典是空的或零。 我提供了我的解决scheme,因为我倾向于更多地关心列举键而不是直接计算条目。

请考虑一下你在其他地方解决问题的事实。
•通过实际填充字典
要么
•修正了mutableDictionary在某种程度上为零的错误

我运行这个testing代码并得到注释的输出

  NSMutableDictionary * countDict = [NSMutableDictionary dictionaryWithObject:@"test" forKey:@"test"]; [countDict setObject:@"foo" forKey:@"bar"]; NSLog(@"test count %d", countDict.count); //test count 2 countDict = nil; NSLog(@"test count %d", countDict.count); //test count 0 

[[dictionary allKeys] count]; 会做的伎俩。

  NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Rakesh",@"Name",@"r@r.com",@"Email",nil]; NSLog(@"Count : %d", [dict count]) 

尝试这个

如果你正在计算任何可以使用的数组项的NSMutableDictionary值。

 NSLog(@"myDictionaryCount %lu", (unsigned long)[myDictionary[@"items"] count]);