2013-11-28 136 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);