Format not a string literal and no format arguments
I'm getting Format not a string literal and no format arguments compilation error.
Here is my code
I'm trying to append NSMutableArray.
for (int i=0; i<feed.count; i+开发者_如何学编程+) {
[html appendFormat:[self.itemName objectAtIndex:i]]; }
Can someone help me please.
You're calling -appendFormat:
with a variable as your format string. This is an extremely bad idea, because the presence of any format specifier (basically, any sequence of "%"
followed by another character) will cause -appendFormat:
to try to read the next argument in the varargs, which you didn't provide, and therefore it will be reading garbage memory off the stack and trying to insert it. At best, this will be a crash. At worst, it's a vector for a security exploit.
You should be using -appendString:
instead.
Note, this assumes that [self.itemName objectAtIndex:i]
is an NSString*
. If it's something else, you may instead want to append its description, e.g. [html appendFormat:@"%@", [self.itemName objectAtIndex:i]]
, or you may want to append something else based on what precise type it is.
The appendFormat: method requires a string as first parameter.
So your selector sending must be something like :
[html appendFormat:@"%@", [self.itemName objectAtIndex:i]];
Though that you could also append directly a string :
[html appendString:[[self.itemName objectAtIndex:i] description]];
I added the description
message in case the object is not directly a string, to be excplicit.
What is this code supposed to do? -[NSMutableString appendFormat:]
takes a format string like @"The %@ says %@"
and “fills in the blanks” with the rest of the arguments.
Some of the more-rarely-used format specifiers are dangerous, in that they can reveal information from your program or cause it to crash (called an uncontrolled format string attack).
Because of this (and because format strings are intended to assemble information from variables), you always want the format string itself to be a literal string, not from a variable or array.
So, you can use appendFormat:
like this:
for (int i=0; i<feed.count; i++) {
[html appendFormat: @"%@", [self.itemName objectAtIndex:i]]; }
Or, you can just use appendString:
instead:
for (int i=0; i<feed.count; i++) {
[html appendString:[self.itemName objectAtIndex:i]]; }
精彩评论