快速枚举使用包含NSDictionary的NSMutableArray

Ala*_*orm 5 cocoa enumeration objective-c 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);
}
Run Code Online (Sandbox Code Playgroud)

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

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

应用程序在没有崩溃的情况下运行,字典将输出到控制台窗口.

这是Fast Enumeration的限制,还是我错过了一些语言的巧妙?嵌套这样的集合时还有其他陷阱吗?

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

and*_*n22 11

哎呀!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);
}
Run Code Online (Sandbox Code Playgroud)

我不确定为什么使用传统的循环隐藏了这个错误.

  • 如果打开-Wformat(Xcode中的"Typecheck调用printf/scanf"),编译器会发出警告.如果你也打开-Werror(Xcode中的"将警告视为错误"),编译器将无法通过此错误进行编译. (3认同)