是否可以对包含 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];
for(id item in myObjects)
{
NSLog(@"Found an Item: %@",item);
}
如果我用传统的计数循环替换快速枚举循环
int count = [myObjects count];
for(int i=0;i<count;i++)
{
id item;
item = [myObjects objectAtIndex:i];
NSLog(@"Found an Item: %@",item);
}
应用程序运行时不会崩溃,并且字典会输出到控制台窗口。
这是快速枚举的限制,还是我错过了该语言的一些微妙之处?嵌套这样的集合时还有其他问题吗?
为了奖励积分,我如何使用 GDB 自己调试这个?
Oops! arrayWithObjects:
需要以零终止。下面的代码运行得很好:
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);
}
我不确定为什么使用传统循环隐藏了这个错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)