使用包含 NSDictionary 的 NSMutableArray 进行快速枚举
是否可以对包含 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 自己调试这个?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀!
arrayWithObjects:
需要以 nil 结尾。以下代码运行良好:我不确定为什么使用传统循环隐藏此错误。
Oops!
arrayWithObjects:
needs to be nil-terminated. The following code runs just fine:I'm not sure why using a traditional loop hid this error.