2011-08-19 100 views
1

我看到這裏,並沒有看到我的相同情況。任何人願意幫忙,謝謝。獲取錯誤「格式不是字符串文字和格式參數」

我有一張分組表,顯示我即將到來的賽季我的橄欖球隊比賽。家庭遊戲和離開遊戲。

 NSString *message = [[NSString alloc] initWithFormat:rowValue]; 
Error Message: Format not a string literal and no format arguments 

真的不知道這是什麼來自?

我使用了一個我在網上找到的教程。複製和粘貼整個事情。我只改變了我個人桌子所需的價值。這是我得到的唯一錯誤?任何幫助?

編輯:如果我需要提供更多的代碼或任何東西,請告訴我!

謝謝!

- Anthony Lombardi

+0

什麼類型的對象/變量是「rowValue」? –

回答

0

不太清楚你的意思。我會輸入一個更大的代碼塊,這可能會有所幫助。

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSArray *listData =[self.tableContents objectForKey: 
        [self.sortedKeys objectAtIndex:[indexPath section]]]; 
NSUInteger row = [indexPath row]; 
NSString *rowValue = [listData objectAtIndex:row]; 

NSString *message = [[NSString alloc] initWithFormat:rowValue]; 

UIAlertView *alert = [[UIAlertView alloc] 
         initWithTitle:@"You Better Be There Or Be Watching It!" 
         message:message delegate:nil 
         cancelButtonTitle:@"Go Knights!" 
         otherButtonTitles:nil]; 
[alert show]; 
[alert release]; 
[message release]; 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
0

有點老問題了,原來提問者很可能早已不復存在......但對於歷史和未來的讀者 - 這裏發生了什麼事情。

問題是因爲您正在創建帶有printf樣式格式字符串的message - initWithFormat預計會有一個格式字符串,其中包含%字符,指示替換 - 例如, %f爲浮點數,%d爲整數等...

而不是靜態格式字符串(字符串文字,直接在您的代碼中定義),您已傳入動態字符串。編譯器很困惑,因爲你沒有傳遞任何參數。如果你的動態字符串包含%替換值,地獄之門就會崩潰,或者最好的是你的應用程序會崩潰。

由於沒有參數的格式字符串沒有意義,所以會引發錯誤。

要解決它,你可以用更換rowValuemessage線:

NSString *rowValue = [listData objectAtIndex:row]; 
NSString *message = rowValue; 

或者更簡潔:

NSString *message = [listData objectAtIndex:row]; 

或者用新的Objective-C的文字語法和下標:

NSString *message = listData[row]; 
相關問題