-2

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.

HardCode
  • 2,025
  • 4
  • 33
  • 55
  • 1
    Not without more information. What type is `feed`? What type is `html`? What type is `itemName` and of what type are its objects? Please give all the details necessary for us to begin to understand what you're doing. – PengOne Oct 11 '11 at 20:38
  • 2
    @PengOne: I think those types are pretty obvious. `feed` is some type of collection (most likely an `NSArray`), `html` is an `NSMutableString`, and `itemName` is again, some type of collection, most likely an `NSArray`. – Lily Ballard Oct 11 '11 at 20:39
  • 1
    @Kevin: The statement "I'm trying to append NSMutableArray" makes much of this unclear. Besides, it's good practice to spell this information out (and had the OP done this he probably could have answered his own question). – PengOne Oct 11 '11 at 20:42
  • -1 for not including any info on the data types involved. – Hot Licks Oct 11 '11 at 20:43
  • Guys I'm sorry if the question is not clear, but I got the answer from Geoffroy! – HardCode Oct 11 '11 at 20:50
  • possible duplicate of [Warning: "format not a string literal and no format arguments"](http://stackoverflow.com/questions/1677824/warning-format-not-a-string-literal-and-no-format-arguments) – jscs Oct 11 '11 at 20:55

3 Answers3

3

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.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
2

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.

Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • Yea you know stackoverflow doesn't let you accept the answer right away! you need to wait at least 10 minutes to accept the answer. I will mark it as answer once it pass the time ;) – HardCode Oct 11 '11 at 20:49
  • 1
    @HardCode: FWIW, you can also use `appendString:` like you were using `appendFormat:` in the question. – s4y Oct 11 '11 at 21:03
2

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]]; }
s4y
  • 50,525
  • 12
  • 70
  • 98