简体   繁体   English

警告:格式不是字符串文字,也没有格式 arguments

[英]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警告:格式不是字符串文字,也没有格式 arguments

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

What i am trying to get as an output is:作为 output,我想要得到的是:

xxxx_110327 xxxx_110327

try:尝试:

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

Assuming qFileTxtName and aString are both defined as NSString* , then the code you've provided does not produce any warning.假设qFileTxtNameaString都定义为NSString* ,那么您提供的代码不会产生任何警告。

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

I think you meant to write -stringByAppendingFormat: , which would produce a warning:我认为您打算编写-stringByAppendingFormat: ,这产生警告:

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:如果您真的想使用-stringByAppendingFormat: ,则需要执行以下操作:

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

This the problem of formater.这是格式化程序的问题。 In newer version of ios sdk there is a new method introdue for this.在 ios sdk 的较新版本中,为此引入了一种新方法。 So use stringWithString instead of stringWithFormat.所以使用 stringWithString 而不是 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

Assuming you have not declared them elsewhere, try假设您没有在其他地方声明它们,请尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM