快速枚举使用NSMutableArray保存NSDictionary [英] Fast Enumeration With an NSMutableArray that holds an NSDictionary

查看:359
本文介绍了快速枚举使用NSMutableArray保存NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对包含NSDictionary的NSArray使用快速枚举?



我正在运行一些Objective C教程,下面的代码踢控制台进入GDB模式

  NSMutableArray * myObjects = [NSMutableArray array]; 
NSArray * theObjects = [NSArray arrayWithObjects:@easy as 1,@easy as two,@Easy as Three];
NSArray * theKeys = [NSArray arrayWithObjects:@A,@B,@C];
NSDictionary * theDict = [NSDictionary dictionaryWithObjects:theObjects forKeys:theKeys];
[myObjects addObject:theDict];

(myObjects中的id项)
{
NSLog(@找到一个项目:%@,item);
}



如果我用传统的计数循环代替快速枚举循环

  int count = [myObjects count]; 
for(int i = 0; i {
id item;
item = [myObjects objectAtIndex:i];
NSLog(@Found an Item:%@,item);
}

应用程序运行时没有崩溃,字典输出到控制台窗口。



这是Fast Enumeration的限制,还是我缺少一些细微的语言?当嵌套这样的集合时还有其他问题吗?



对于奖励积分,我如何使用GDB自己调试?

解决方案

糟糕! arrayWithObjects:需要nil终止。以下代码运行正常:

  NSMutableArray * myObjects = [NSMutableArray array]; 
NSArray * theObjects = [NSArray arrayWithObjects:@easy as 1,@easy as two,@Easy as Three,nil];
NSArray * theKeys = [NSArray arrayWithObjects:@A,@B,@C,nil];
NSDictionary * theDict = [NSDictionary dictionaryWithObjects:theObjects forKeys:theKeys];
[myObjects addObject:theDict];

(myObjects中的id项)
{
NSLog(@找到一个项目:%@,item);
}



我不知道为什么使用传统的循环会隐藏这个错误。 p>

Is it possible to use fast enumeration with an NSArray that contains an NSDictionary?

I'm running through some Objective C tutorials, and the following code kicks the console into GDB mode

NSMutableArray *myObjects = [NSMutableArray array];
NSArray *theObjects = [NSArray arrayWithObjects:@"easy as 1",@"easy as two", @"Easy as Three"];
NSArray *theKeys    = [NSArray arrayWithObjects:@"A",@"B",@"C"];    
NSDictionary *theDict = [NSDictionary dictionaryWithObjects:theObjects forKeys:theKeys];
[myObjects addObject:theDict];

for(id item in myObjects)
{
    NSLog(@"Found an Item: %@",item);
}

If I replace the fast enumeration loop with a traditional counting loop

int count = [myObjects count];
for(int i=0;i<count;i++)
{
    id item;
    item = [myObjects objectAtIndex:i];
    NSLog(@"Found an Item: %@",item);
}

The application runs without a crash, and the dictionary is output to the console window.

Is this a limitation of Fast Enumeration, or am I missing some subtly of the language? Are there other gotchas when nesting collections like this?

For bonus points, how could I used GDB to debug this myself?

解决方案

Oops! arrayWithObjects: needs to be nil-terminated. The following code runs just fine:

NSMutableArray *myObjects = [NSMutableArray array];
NSArray *theObjects = [NSArray arrayWithObjects:@"easy as 1",@"easy as two", @"Easy as Three",nil];
NSArray *theKeys    = [NSArray arrayWithObjects:@"A",@"B",@"C",nil];    
NSDictionary *theDict = [NSDictionary dictionaryWithObjects:theObjects forKeys:theKeys];
[myObjects addObject:theDict];

for(id item in myObjects)
{
    NSLog(@"Found an Item: %@",item);
}

I'm not sure why using a traditional loop hid this error.

这篇关于快速枚举使用NSMutableArray保存NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆