2012-01-27 131 views
0

好吧,所以我知道我可以忽略NSLog,但爲什麼它會給我一個「EXC_BAD_ACCESS」錯誤?NSLog給出運行時錯誤

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex2 
{ 

    if(buttonIndex2 == 0 && waitForAction == NO) 
    { 
     waitForAction = YES; 
     [self showAbortAlert]; 
     NSLog(@"%@",buttonIndex2); //This one does not crash the app 

    } else if (buttonIndex2 == 1 && waitForAction == NO) 
    { 
     waitForAction = YES; 
     [self addObject]; 
     NSLog(@"%@",buttonIndex2); //This one crashes the app 
    } //else if 
} 

回答

3

再次看到方法簽名

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex2 

buttonIndex2的類型爲NSInteger。如果你在NSLog中做%@,你的代碼調用對象上的description方法。但buttonIndex2不是一個對象。

使用NSLog(@"%d",buttonIndex2);

第一個(與buttonIndex == 0),因爲你正在呼籲在內存地址0,這是基本相同[nil description],這是完全合法的對象上description不會崩潰的應用程序在Objective-C中。

+0

謝謝!我誤解了這一點。 – 2012-01-27 11:32:17

+0

或'%i'(我個人喜好導致我認爲int不是十進制)。 – vakio 2012-01-27 11:33:50

2

使用%d,因爲它是一個整數,%@是串...

NSLog(@"%d", buttonIndex); 
2

buttonIndex2是一個整數使用:

NSLog(@"%d",buttonIndex2); 

而且去閱讀基於C一些好書

相關問題