PeterK
PeterK

Reputation: 4301

warning: format not a string literal and no format arguments

Could someone help me to understand why the code-line below return:

warning: format not a string literal and no format arguments

qFileTxtName = @"110327";
aString = [@"xxxx_" stringByAppendingString:qFileTxtName];

What i am trying to get as an output is:

xxxx_110327

Upvotes: 0

Views: 1143

Answers (4)

Bhoopi
Bhoopi

Reputation: 6593

This the problem of formater. In newer version of ios sdk there is a new method introdue for this. So use stringWithString instead of stringWithFormat. As below.

 NSMutableArray * array_mutable = [NSMutableArray arrayWithObject:@"tekst"];
        NSString *s1;
        s1 = [NSString stringWithFormat:[myArray objectAtIndex:0]]; //(Format string is not a string literal (potentially insecure)
        s1 = [NSString stringWithString:[myArray objectAtIndex:0]]; //no problem

Upvotes: 0

NSGod
NSGod

Reputation: 22948

Assuming qFileTxtName and aString are both defined as NSString*, then the code you've provided does not produce any warning.

NSString *qFileTxtName = @"110327";
NSString *aString = [@"xxxx_" stringByAppendingString:qFileTxtName];
// no warnings

I think you meant to write -stringByAppendingFormat:, which would produce a warning:

NSString *qFileTxtName = @"110327";
NSString *aString = [@"xxxx_" stringByAppendingFormat:qFileTxtName]; 
// warning: format not a string literal and no format arguments

If you really wanted to use -stringByAppendingFormat:, you'd need to do something like this:

NSString *aString = [@"xxxx_" stringByAppendingFormat:@"%@", qFileTxtName]; 

The following logging calls show another operation that would result in that warning from the compiler, and the better (more secure) way to code it:

NSLog(aString); // warning: format not a string literal and no format arguments

NSLog(@"%@", aString); // the more secure way to do it

Upvotes: 2

PengOne
PengOne

Reputation: 48398

Assuming you have not declared them elsewhere, try

NSString *qFileTxtName = @"110327";
NSString *aString = [@"xxxx_" stringByAppendingString:qFileTxtName];

Upvotes: 0

Jesse Naugher
Jesse Naugher

Reputation: 9820

try:

NSString *aString = @"xxxx_";
aString = [aString stringByAppendingString:qFileTextName];

Upvotes: 3

Related Questions