Format string is not a string literal.

Discussion in 'Mac Programming' started by larswik, Jan 17, 2014.

  1. larswik macrumors 68000

    Sep 8, 2006
    When my program is not able to move a file to a different location I add the file name to an NSMutableArray. When the program finishes, and the array has a count of more the 0, I have an NSAlert display on the screen with a list of files that did not transfer. It works just fine, or as expected, but I get this warning. The part of the alert that takes the argument takes an NSString. I created an NSString but I get the error.

        NSString *myArrayString = [[canNotTransferArray valueForKey:@"description"]  componentsJoinedByString:@", "];
        NSAlert *alert = [NSAlert alertWithMessageText:@"Files not copied because of duplicate names." defaultButton:@"Ok" alternateButton:nil otherButton:nil informativeTextWithFormat:myArrayString];
        [alert setAlertStyle:NSInformationalAlertStyle];
        [alert runModal];
    I did a Google search and found a number of these problems to figure it out first. I did try to use [NSString stringWithFormat:mad:"%@,] but it still gave me the same problem. I'm pretty sure it is a formatting issue but I just can't put my finger on it.

    Any help at this point would be appreciated.
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    Your first line confuses me... You're getting the description of an array, which is a string. Then you're trying to join components together, but that doesn't make sense because you have a string at that point, not an array.

    Can you give more details on your error? Is it occurring at runtime or compile time? Which line is the error occurring on?
  3. ohbrilliance macrumors 6502a


    May 15, 2007
    Melbourne, Australia
    componentsJoinedByString is an instance method of NSArray. Is the object returned by canNotTransferArray valueForKey:mad:"description" an instance of NSArray?

    If not then componentsJoinedByString will return nil, probably not what you expected.
  4. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    The "informativeTextWithFormat:" argument expects a literal format string. The method then accepts variable arguments for the parameters that fill in placeholders. You're generating a formatted string and feeding it in. If you inadvertently passed format specifiers you'd have problems. The format @"%@" and your current string following it should do it.

  5. gnasher729 macrumors P6


    Nov 25, 2005
    The last parameter is a format string, followed by optional parameters. Similar to printf. For example it could be:

    informativeTextWithFormat:mad:"x = %d, y = %d", x, y

    So the format string should be a string literal, to allow the compiler to check it. In this example, the compiler will see two %d in the string and check that there are two int values.

    If it isn't a string literal, then you are in danger - if the string contains for example %d, then it will want an integer value to substitute for the %d, which most likely will produce rubbish. If the string contains %s or %@, then your program will most likely crash.

    Passing in [NSString stringWithFormat... ] just gives the same problem again.

    Pass as the last parameter:

    informativeTextWithFormat:mad:"%@", myArrayString
  6. larswik thread starter macrumors 68000

    Sep 8, 2006
    Thanks guys, you pointed out something I didn't even notice. I was so focused on that it was an NSString and that was the parameter that I was providing.

    I then 'READ THE DOCS' completely and the keyword "Format" jumped out at me, as in Format specifier. Which then the docs had a link, "For more information on format strings, see “Formatting String Objects”." and the examples you had made sense.

    I revised my Method to this and now it works as expected with errors.

        NSString *myArrayString = [canNotTransferArray description];
        NSAlert *alert = [NSAlert alertWithMessageText:@"Files not copied because of duplicate names." defaultButton:@"Ok" alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@", myArrayString];
        [alert setAlertStyle:NSInformationalAlertStyle];
        [alert runModal];
    Thanks for the help folks....

Share This Page