2013-11-28 205 views
-1

为什么我在NSLog(@"%@", numbers[i]);线路上出现线程错误?线程1:信号sigabrt错误

#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; 
} 
+0

这当然不是一个Xcode的问题。 – 2013-11-28 07:31:54

+1

此外,您可以**读取(整个)异常消息 - 问题是您正在访问数组越界。 – 2013-11-28 07:33:16

+0

如果你原谅了观察,虽然我很高兴你得到了你的问题的答案,但是,在将来,如果你发布有关错误或崩溃的问题,请务必发布完整的错误消息和堆栈跟踪。见[我的应用程序崩溃,现在什么?](http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1)。 – Rob

回答

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