2013-07-17 35 views
-3

我想從我的NSMutableArray獲取數據,如果我嘗試這個NSLog(@"%@",[A_array objectAtIndex:5]); // works fine如何從整數值中獲取來自NSMutableArray ObjectAtIndex的數據?

NSLog(@"%@",[A_array objectAtIndex:var_Count_Answer_A]); // var_Count_Answer_A =5; and crash 

任何想法或建議,將不勝感激我這樣做

-(void)ViewDidLoad{ 
    A_array = [[NSMutableArray alloc] init]; 
    A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil] 
    var_Count_Answer_A = 0; // int 
} 

-(void)method_Second { 
    NSLog(@"%@",[A_array objectAtIndex:var_Count_Answer_A]); // This line is crashing if I click button again. First time line works fine but if we click button again and var_Count_Answer = 2 then it will crash. 

    NSString *str1 = [[NSString alloc]initWithFormat:@"%@",[A_array objectAtIndex:var_Count_Answer_A]]; // If I comment on NSLOG then this line will crash 
    A = [str1 integerValue]; 
    NSLog(@"A is %d",A); 


    var_Count_Answer_A ++; 
} 

+2

發表您的崩潰日誌。 – Balu

+0

其中是定義var_Count_Answer_A –

+0

您是否使用ARC?這些iVars在哪裏定義? – Stavash

回答

1

使用

A_array = [[NSMutableArray alloc] initWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil]; 

相反,你使用。

A_array = [[NSMutableArray alloc] init]; 
    A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil]; 

當我檢查您的項目是ARC禁用試試這對我工作很好。

ViewControler.h 
@interface MasterViewController : UIViewController 
{ 
    NSMutableArray *A_array; 
    int var_Count_Answer_A; 
} 

ViewControler.m 
-(void)viewDidLoad { 
    [super viewDidLoad]; 
    A_array = [[NSMutableArray alloc] initWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil]; 
var_Count_Answer_A = 0; 
    var_Count_Answer_A = 0; // int 
     } 

-(IBAction)method_Second:(id)sender { 
    NSLog(@"%@",[A_array objectAtIndex:var_Count_Answer_A]);  

    NSString *str1 = [[NSString alloc]initWithFormat:@"%@",[A_array objectAtIndex:var_Count_Answer_A]]; // If I comment on NSLOG then this line will crash 
    int A = [str1 integerValue]; 
    NSLog(@"A is %d",A); 

    var_Count_Answer_A++; 
     } 

但是正如大家都知道最後一定會發生這種例外。

NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 16 beyond bounds [0 .. 15]' 

由於試圖訪問不存在的數組索引對象。 And if you still have some error then First Enable NSZombie in your project Follow this.

- >「Product」菜單,選擇「Edit Scheme」,轉到左邊面板中的「YourAp.app」階段,右邊的「Arguments」選項卡。然後,您可以將NSZombieEnabled添加到「Environment Variables」部分,並將值設置爲YES

And Now Share your Crash Log.

+0

此錯誤正在顯示 - 符號存根:getpid –

+0

請啓用NSZombie,因爲我還更新了我的答案並共享了崩潰日誌。 – Buntylm

+0

- [__ NSArrayM objectAtIndex:]:發送到釋放實例的消息0x71c07a0 –

0

在您的代碼中,您一旦檢查了thosetwo,就會使用兩個不同的varaibles。

var_Count_Answervar_Count_Answer_A

+0

對不起,先生錯誤,我忘了寫_A這裏在我的代碼都只有var_Count_Answer_A。 –

+0

這個錯誤正在顯示 - 符號存根:getpid –

+0

@ Nisha Singh:你的代碼似乎是很好的堅果一些地方你錯了我認爲如此。 – Balu

3

您的代碼傷害我的眼睛

有一定的編碼標準,而在Objective-C編寫代碼來遵循。每種語言都有一個。他們使代碼易讀易懂。你可以參考Cocoa Coding Guidelines並幫助你自己編寫更好看的代碼。

解釋正確

沒有錯誤消息,飛機墜毀前沒有人可以幫你。下次當你發佈關於崩潰的消息時,還要解釋你得到的錯誤消息。

是不是EXEC_BAD_ACCESS?如果是這樣的話,真棒垃圾收集器將清除你的數組對象。無論如何,你的代碼令我感到眼花繚亂,我正在重構代碼。也許它會幫助你。

重構

NSArray *aArray; 
- (void)viewDidLoad 
    { 
    [super viewDidLoad]; 
    aArray = @[@"1", @"2", @"3", @"4"]; 
    // or use[[NSArray alloc]initWithObjects:@"1", @"2", @"3", @"4", @"5"]; 
    //Why do you need a mutable array while initializing it static? 
    } 

- (void)methodSecond 
    { 
    static int counter = 0; 
     if (aArray) { 
     if (counter<aArray.count) { 
      NSLog(@"A is %u",[[aArray objectAtIndex:counter++]integerValue]); 
     } else { 
      counter = 0; 
      [self methodSecond]; 
     } 
     } 
    } 

一點點的瞭解你在做什麼

那麼什麼我猜你的問題詳細解釋是:

  • 你」重新使用[NSArray initWithObjects:]這會創建一個自動釋放對象,我們沒有控制其使用壽命。如果在創建之後沒有立即引用它們,它們會被釋放,這可能是您的情況。根據我的經驗,可變自動釋放對象總是會導致訪問問題。所以最好有對象alloc和inited,即.. [[NSArray alloc] initWithObjects:],我注意到你沒有在運行時添加/刪除數組成員。沒有MutableArray的目的。

  • 在你的代碼中,你正在創建兩個對象。第一個數組對象被分配並立即在下一行中解除引用。這可能是一個錯誤。

如果您想了解更多關於自動釋放對象的信息。閱讀this

+0

謝謝alooot ...你的代碼太棒了。它的工作精湛,正是我想要的......謝謝Alooottt .... –

0

發生此錯誤是因爲您正在使用自動發佈的對象。 當你調用,如:

A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil] 

的A_array是一個自動釋放的對象,你不能說什麼時候它會被釋放。這就是發生這種錯誤的原因。

解決方案:

聲明屬性像數組:

@property (nonatomic, strong) NSMutableArray *A_array; 

然後修改你的方法,如:

-(void)ViewDidLoad 
{ 
    _A_array = [NSMutableArray arrayWithObjects:@"4",@"6",@"2",@"3",@"0",@"5",@"1",@"2",@"4",@"1",@"0",@"2",@"4",@"2",@"0",@"3",nil] 
    var_Count_Answer_A = 0; // int 
} 

-(void)method_Second 
{ 
    NSLog(@"%@",[_A_array objectAtIndex:var_Count_Answer_A]) 
    NSString *str1 = [[NSString alloc]initWithFormat:@"%@",[_A_array objectAtIndex:var_Count_Answer_A]]; 
    A = [str1 integerValue]; 
    NSLog(@"A is %d",A); 
    var_Count_Answer_A ++; 
} 
相關問題