Objective-C循环数组

pro*_*vdr 2 cocoa loops objective-c

我有类似的代码

if (count == 0)
{
    [array1 addObject:@"No Items"];
} 
else
{
    int x;
    for (x = 0;x <= count; x++) 
    {
            [array1 addObject:[itemsArray objectAtIndex:x];
            NSLog(@"%@",array1);
    }
}
Run Code Online (Sandbox Code Playgroud)

itemsArray有数字(0-40).我的预期结果是:

  • 1
  • 2
  • 3
  • ...

但实际上它确实:

  • 1
  • 1,2
  • 1,2,3
  • 1,2,3,4
  • 1,2,3,4,5
  • ...

为什么会这样?如果可能的话,我还想问一个例子,为这种情况使用快速枚举(如果它适合这个).

提前致谢.

Mat*_*ows 18

你是NSLog整个数组,而不是array1的当前索引.您所看到的记录是您编码的内容 - 记录您期望的内容,更改NSLog(@"%@",array1);NSLog(@"%@",[array1 objectAtIndex:x]);

要确认在分配循环后添加以下内容:

for (NSObject* o in array1)
{
    NSLog(@"%@",o);
}
Run Code Online (Sandbox Code Playgroud)