Getting the error "Format not a string literal and no format arguments"
I looked on here and didnt see my same situation. Anyone willing to help, thanks.
I have a Grouped Table that displays my football teams games this upcoming season. Home 开发者_StackOverflowGames and Away Games.
NSString *message = [[NSString alloc] initWithFormat:rowValue];
Error Message: Format not a string literal and no format arguments
Not really even sure what this comes from?
I used a tutorial I found online. Copied and pasted the whole thing. I only changed the values I need for my personal table. This is the only error I get? Any help??
Edit: If I need to supply more code or anything, please, let me know!
Thank you!!
-- Anthony Lombardi
Not quite sure what you mean. I'll input a bigger block of code, that might help.
- (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];
}
Somewhat old question, and the original asker is likely long gone... but for history and future readers - here's what's going on.
The problem is because you're creating message
with a printf style format string - initWithFormat
expects to have a format string with % characters indicating the substitutions - e.g. %f for a float, %d for an integer etc...
Instead of a static format string (a string literal, defined in your code directly) you've passed in a dynamic string. The compiler is confused, because you haven't passed any arguments. If your dynamic string contained % substitutions, the gates of hell would break loose, or at the very best you're apps going to crash.
Since it makes no sense to have a format string with no arguments, it raises the error.
To fix it you can replace the rowValue
and message
lines with:
NSString *rowValue = [listData objectAtIndex:row];
NSString *message = rowValue;
Or more concisely:
NSString *message = [listData objectAtIndex:row];
Or using new objective-c literals syntax and subscripting:
NSString *message = listData[row];
精彩评论