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

发布于 2024-12-27 23:18:22 字数 589 浏览 6 评论 0 原文

将 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 执行。

然而单步执行一行: a __NSCFString * 0x6818b70

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

尖端?

After loading a plist into an NSArray I'm trying to access its nested arrays.

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]);
}

Some values from the debugger:

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

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

However stepping one line:
a __NSCFString * 0x6818b70

And boom, appcrash.

Tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

¢蛋碎的人ぎ生 2025-01-03 23:18:22

所以我希望 for 语句跳过前 2 个 NSString,然后使用 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

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

That is not what the for (NSArray *a in tree) expression does. That statement creates a local variable named a of type NSArray * and assigns it to reference each object in tree, regardless of if the object at a particular index is an NSArray or not.

Your fast enumeration loop is roughly equivalent to:

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

揪着可爱 2025-01-03 23:18:22

正如乔纳(Jonah)已经指出的那样,仅仅告诉编译器您期望一个 NSArray 并不能真正使其成为一个。如果您想跳过循环中不是数组的对象,可以按如下方式执行:

for (NSArray *a in tree) {
    if (![a isKindOfClass:[NSArray class]]) continue;
    //...
}

As Jonah already pointed out, just telling the compiler that you expect an NSArray doesn't actually make it one. If you want to skip objects that aren't arrays in your loop, you could do it as follows:

for (NSArray *a in tree) {
    if (![a isKindOfClass:[NSArray class]]) continue;
    //...
}
去了角落 2025-01-03 23:18:22

作为 omz & Jonah 指出您的 for 循环并不只是通过指定 a 的类型来选择项目。这是另一种变体,它清楚地表明元素可能不是数组:

for (id element in tree)
{
   // skip non-arrays
   if (![element isKindOfClass:[NSArray class]]) continue;

   NSArray *a = (NSArray *)element;
   ...
}

As omz & Jonah have pointed out your for loop does not also select items simply by specifying the type of a. Here is another variation which makes it clear the elements may not be arrays:

for (id element in tree)
{
   // skip non-arrays
   if (![element isKindOfClass:[NSArray class]]) continue;

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