使用包含 NSDictionary 的 NSMutableArray 进行快速枚举

发布于 2024-08-23 00:58:09 字数 855 浏览 5 评论 0原文

是否可以对包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-08-30 00:58:09

哎呀! 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];

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

我不确定为什么使用传统循环隐藏此错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文