Ios NSString stringWithFormat格式警告
我可能使用了错误的符号将NSString类型包含在另一个字符串中Ios NSString stringWithFormat格式警告,ios,nsstring,Ios,Nsstring,我可能使用了错误的符号将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 str
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)
有没有办法消除警告?根据实现,函数的第一个参数应该是常量字符串或格式说明符字符串
您需要使用:
NSLog(@"Problem with SQL statement in [BSurfSpotDBOperations allSpots] \n %@ \n %@",sqlQuery,[NSString stringWithUTF8String:error]);
根据实现,函数的第一个参数是常量字符串或格式说明符字符串
您需要使用:
NSLog(@"Problem with SQL statement in [BSurfSpotDBOperations allSpots] \n %@ \n %@",sqlQuery,[NSString stringWithUTF8String:error]);
NSLog()
语句中的格式字符串必须是常量字符串,否则将出现警告。重新编写代码,使格式部分成为字符串文字
通常避免使用复杂的复合语句。临时变量使代码更具可读性,并可能减少错误。编译器非常擅长在发布编译时消除临时变量
例如:
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);
NSLog()
语句中的格式字符串必须是常量字符串,否则将出现警告。重新编写代码,使格式部分成为字符串文字
通常避免使用复杂的复合语句。临时变量使代码更具可读性,并可能减少错误。编译器非常擅长在发布编译时消除临时变量
例如:
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);
stringWithFormat
在这里是不必要的,因为NSLog()
提供了类似的选项。您可以简单地将其更改为:
NSLog(@"Problem with ... \n %@ \n %@", sqlQuery,[NSString stringWithUTF8String:error]);
这将消除警告,因为NSLog知道它不会遇到字符串文本,它不知道如何处理它,这是一个安全问题。
stringWithFormat
在这里是不必要的,因为NSLog()
提供了类似的选项。您可以简单地将其更改为:
NSLog(@"Problem with ... \n %@ \n %@", sqlQuery,[NSString stringWithUTF8String:error]);
这将消除警告,因为NSLog知道它不会遇到字符串文本,它不知道如何处理它,这是一个安全问题。尝试此方法:
NSLog([@"Problem with SQL statement in [BSurfSpotDBOperations allSpots] \n %@ \n %@"
,sqlQuery,[NSString stringWithUTF8String:error]);
我想您也可以通过error
来翻译[nsstringwithutf8string:error]
我认为这对你有帮助;) 试试这个方法:
NSLog([@"Problem with SQL statement in [BSurfSpotDBOperations allSpots] \n %@ \n %@"
,sqlQuery,[NSString stringWithUTF8String:error]);
我想您也可以通过error
来翻译[nsstringwithutf8string:error]
我认为这对你有帮助;) 首先,发布的代码缺少一个尾随“]”。总是分解复合语句,特别是在调试时<代码>NSLog()在每个步骤之后,或在每个步骤之后使用调试器进行检查。;您是否在
NSLog()
中有完整的语句?是的,它在NSLog中,请参阅编辑摆脱stringWithFormat:
的两种用法,并使用%s
而不是%@
来处理错误
。首先,发布的代码缺少一个尾随“]”。总是分解复合语句,特别是在调试时<代码>NSLog()在每个步骤之后,或在每个步骤之后使用调试器进行检查。;你在一个NSLog()中有完整的语句吗?
?是的,它在NSLog中,请参阅编辑摆脱stringWithFormat:
的两种用法,并使用%s
而不是%@
来记录错误
。更好的是-摆脱错误字符串
,使用%s
直接记录错误
。不需要NSString
。从调试POV中,额外的语句可能会暴露错误,可能它没有真正设置,没有以null结尾,等等。请注意,错误语句没有包含在原始问题中。通常,在调试存在错误的假设时,验证每个步骤都有助于暴露错误的假设。更好的做法是,去掉errorString
,并使用%s
直接记录错误
。不需要NSString
。从调试POV中,额外的语句可能会暴露错误,可能它没有真正设置,没有以null结尾,等等。请注意,错误语句没有包含在原始问题中。通常,当调试存在错误的假设时,验证每个步骤有助于暴露错误的假设。