Warning: format is not a string literal and format arguments

Can someone help me understand why the line of code is returned below:

warning: format is not a string literal and format arguments

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

What I'm trying to get as output:

xxxx_110327

0
source share
4 answers

to try:

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

Assuming qFileTxtName and aString defined as NSString* , then the code you provided does not raise any warnings.

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

I think you wanted to write -stringByAppendingFormat: which would -stringByAppendingFormat: 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 need to do something like this:

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

The following log calls show another operation that will trigger this warning from the compiler and a better (more secure) way to encode it:

 NSLog(aString); // warning: format not a string literal and no format arguments NSLog(@"%@", aString); // the more secure way to do it 
+2
source

Assuming you haven’t announced them elsewhere, try

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

This is a formatting issue. In the new version of ios sdk there is a new method for this. So use stringWithString instead of stringWithFormat. As shown 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 
0
source

Source: https://habr.com/ru/post/1440928/


All Articles