gpt4 book ai didi

objective-c - for (NSArray *a in directory) 无法按我的预期工作

转载 作者:行者123 更新时间:2023-12-03 17:28:03 25 4
gpt4 key购买 nike

将 plist 加载到 NSArray 后,我尝试访问其嵌套数组。

NSArray *tree = [[NSArray alloc] initWithContentsOfFile:path];
for (NSArray *a in tree)
{
//Let's assume object at index 0 is always NSString
NSLog(@"Returning the string: %@ ", [a objectAtIndex:0]);
}

来自调试器的一些值:

   tree __NSCFArray *   0x6856cf0
0 __NSCFString * 0x6818b70
1 __NSCFString * 0x682be10
2 __NSCFArray * 0x6856cd0

所以我希望 for 语句跳过前 2 个 NSString,然后使用 NSArray 执行。

但是单步执行一行:__NSCFString * 0x6818b70

然后繁荣,应用程序崩溃。

提示?

最佳答案

So I'm expecting the for statement to skip the first 2 NSStrings and then execute with the NSArray.

这不是 for (NSArray *a in tree) 表达式的作用。该语句创建一个名为 a 且类型为 NSArray * 的局部变量,并将其指定为引用 tree 中的每个对象,无论该对象是否位于 a特定索引是否为 NSArray。

您的快速枚举循环大致相当于:

NSArray *a;
for (NSInteger index = 0; index < [tree count]; index++) {
a = [tree objectAtIndex:index];
...
}

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocFastEnumeration.html

关于objective-c - for (NSArray *a in directory) 无法按我的预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8951904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com