2012-10-15 27 views
0

我已經查看了一段時間的網站,我看到了一些東西,但沒有爲我工作。儘管這裏是極端新手。NSInvalidArgumentException,無參數

我試圖做一個2組件選擇器,從數組填充,從字典填充,從plist填充。組件填充,並且我有一個可以旋轉一個組件的按鈕。第二個按鈕是爲了檢查答案(第一個組件具有他們試圖與第二個組件中的大寫匹配的狀態),但這是它總是崩潰的地方。

我得到的錯誤如下:

*終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是: '* - [NSPlaceholderString initWithFormat:區域:參數:]:無參數'

*第一擲調用堆棧: (0x14b3022 0xeb3cd6 0x145ba48 0x145b9b9 0x916169 0x916ab4 0x3036 0x14b4e99 0x1914e 0x190e6 0xbfade 0xbffa7 0xbf266 0x3e3c0 0x3e5e6 0x24dc4 0x18634 0x139def5 0x1487195 0x13ebff2 0x13ea8da 0x13e9d84 0x13e9c9b 0x139c 7D8 0x139c88a 0x16626 0x27ad 0x2715) 終止叫做拋出異常(LLDB)

我不知道我要去哪裏錯了。我用硬編碼的數組完成了這個工作,並且它工作正常。該按鈕的代碼是這樣的(請忽略開關代碼,我還沒有完全弄清楚如何完成這項工作,我希望屏幕變爲綠色以獲得正確的答案,而紅色以獲得正確答案,但我更關心與應用程序崩潰!):

- (IBAction)checkAnswer; { NSInteger StateRow = [pickerCapital selectedRowInComponent:karrayStateComponent]; NSInteger CapitalRow = [pickerCapital selectedRowInComponent:karrayCapitalComponent];

NSString *state = [arrayState objectAtIndex:StateRow]; 
NSString *capital = [arrayCapital objectAtIndex:CapitalRow]; 

NSLog (@"%i",[pickerCapital selectedRowInComponent:karrayCapitalComponent]); 
NSLog(@"%i", [pickerCapital selectedRowInComponent:karrayStateComponent]); 

NSString *Title = [[NSString alloc]initWithFormat:@"You have selected %@ as the capital of %@.", capital, state]; 
NSString *Message =[[NSString alloc]initWithFormat:lblMsg]; 

UIAlertView *alert =[[UIAlertView alloc]initWithTitle:Title message:Message delegate:@"" cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[alert show]; 

if (StateRow == CapitalRow) { 
    lblMsg = @"You are correct!"; 
    UIColor *myColor; 
    switch ([not sure what to use here]) { 
     case 0: 
     myColor = [UIColor greenColor]; 
      break; 
     default: 
      break; 
    } 
} 
else { 
    lblMsg = @"Incorrect"; 
    UIColor *myColor; 
    switch ([not sure what to use here]) { 
     case 0: 
     myColor = [UIColor redColor]; 
      break;   
     default: 
      break; 
    } 
} 

}

如果任何人都可以提供這個任何形式的幫助,我將不勝感激!謝謝!

+0

您需要設置異常斷點,以便可以看到引發異常的位置。看看這個問題的答案:http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4 –

+0

啊,好的! NSString * Message發生異常,特別是lblMsg。它說「格式化字符串不是字符串文字(可能不安全)」。我在其他按鈕上出現了同樣的黃色錯誤,但它從未形成例外。 – user1678809

回答

0

兩件事情來檢查:

  • 凡lblMsg聲明或賦值?
  • 你確定大寫和狀態不是來自[arrayCapital objectAtIndex]的調用嗎?

具體來說:the NSString documentation states「重要事項:如果格式爲nil,則引發NSInvalidArgumentException。爲initWithFormat。

+0

我沒有將lblMsg聲明爲NSString,它從if語句中獲取它的值。我不相信他們是零,就像我說的,它在硬編碼數組中工作得很好。那是我在viewDidUnload上釋放數組時發生的事情嗎? – user1678809

相關問題