[英]How to copy NSMutableDictionary values into the another NSMutableDictionary in iPhone?
[英]How to get NSMutableDictionary count in iphone?
我想在iPhone中获取NSMutableDictionary计数。 我想知道NSMutableDictionry中有多少个项目。 我尝试了这些代码来找出解决方案,但是对我没有多大帮助。
NSLog(@"Count : %d", [mutableDictionary count]);
它始终返回“ 0”。 如何获取iPhone中NSMutableDictionary的计数? 提前致谢。
您可以找到有多少个键-对象(键-值)对,如下所示:
NSArray * allKeys = [mutableDictionary allKeys];
NSLog(@"Count : %d", [allKeys count]);
编辑
浏览字典文档后, NSDictionary
的count
方法(或属性)也应该起作用。 我认为您可能因为字典为空或nil而收到了0计数。 之所以提供解决方案,是因为我倾向于更关心枚举键,而不是直接计算条目。
请考虑您在其他地方解决了这个问题。
•通过实际填充字典
要么
•修复了mutableDictionary无效的错误
我运行此测试代码并获得评论输出
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])
尝试这个
如果您希望为可以使用的任何Array项计算NSMutableDictionary值。
NSLog(@"myDictionaryCount %lu", (unsigned long)[myDictionary[@"items"] count]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.