线程1:信号sigabrt错误

时间:2013-11-28 07:27:51

标签: objective-c

为什么我会在NSLog(@"%@", numbers[i]);行上收到错误?

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

    NSMutableArray *numbers = [NSMutableArray array];
    int i;

    //Create an arry with the number 0-9

    for (i = 0; i < 10; ++i) {
        numbers[i] = @(i);

        //Sequence through the array and display the values

        for (i = 0; i < 10; ++i) {
            NSLog(@"%@", numbers[i]);

            //Look how NSLog can display it with a singe %@ format

            NSLog(@"====== Using a single NSLog");
            NSLog(@"%@", numbers);
        }
    }

}
return 0;
}

1 个答案:

答案 0 :(得分:0)

您收到异常是因为您嵌套了两个for循环,而内部循环试图遍历numbers数组中的十个值,但是在您尝试之前尝试执行此操作完成在外循环中填充数组。

我认为你不希望这些for循环嵌套:

NSMutableArray *numbers = [NSMutableArray array];
int i;

//Create an array with the number 0-9

for (i = 0; i < 10; ++i)
    numbers[i] = @(i);

//Sequence through the array and display the values

for (i = 0; i < 10; ++i) 
    NSLog(@"%@", numbers[i]);

//Look how NSLog can display it with a single %@ format

NSLog(@"====== Using a single NSLog");
NSLog(@"%@", numbers);