对于将NSString类型包含在另一个字符串中,我可能使用了错误的符号。
const char* error; //it's set, not nil
NSLog([NSString stringWithFormat : @"Problem with SQL statement in \n %@ \n %@",sqlQuery,[NSString stringWithUTF8String:error]]);这个表达式有效,但我收到了这样的警告:
Formatting string is not a string literal(potentially insecure)有办法消除警告吗?
发布于 2014-06-06 20:10:11
根据NSLog实现,函数需要一个常量字符串或格式说明字符串作为第一个参数。
你需要使用:
NSLog(@"Problem with SQL statement in [BSurfSpotDBOperations allSpots] \n %@ \n %@",sqlQuery,[NSString stringWithUTF8String:error]);发布于 2014-06-06 20:10:49
NSLog()语句中的字符串格式必须为常量字符串,否则将出现警告。重写代码,使格式部分成为字符串文本.
通常避免复杂的复合语句。临时变量使代码更加可读性更强,潜在的buggy也更少。编译器很擅长在发布编译时消除临时代码.
示例:
const char* error; //it's set, not nil
NSString *errorString = [NSString stringWithUTF8String:error];
NSLog(@"Problem with SQL statement in [BSurfSpotDBOperations allSpots] \n %@ \n %@", sqlQuery, errorString);发布于 2014-06-06 20:10:57
stringWithFormat在这里是不必要的,因为NSLog()提供了类似的选项。您只需将其更改为:
NSLog(@"Problem with ... \n %@ \n %@", sqlQuery,[NSString stringWithUTF8String:error]);这将消除警告,因为NSLog知道它不会遇到字符串文字,它不知道如何处理,这是一个安全问题。
https://stackoverflow.com/questions/24089790
复制相似问题