警告:“格式不是字符串文字,并且没有格式参数" [英] Warning: "format not a string literal and no format arguments"
问题描述
自从升级到最新的Xcode 3.2.1和Snow Leopard以来,我一直收到警告
Since upgrading to the latest Xcode 3.2.1 and Snow Leopard, I've been getting the warning
格式不是字符串文字,并且没有格式参数"
"format not a string literal and no format arguments"
来自以下代码:
NSError *error = nil;
if (![self.managedObjectContext save:&error])
{
NSLog([NSString stringWithFormat:@"%@ %@, %@",
errorMsgFormat,
error,
[error userInfo]]);
}
如果errorMsgFormat
是带有格式说明符(例如:"print me like this: %@"
)的NSString
,那么上面的NSLog
调用怎么了?推荐的解决方法是什么,以免产生警告?
If errorMsgFormat
is an NSString
with format specifiers (eg: "print me like this: %@"
), what is wrong with the above NSLog
call? And what is the recommended way to fix it so that the warning isn't generated?
推荐答案
您是否正确嵌套了方括号?我不认为NSLog()
喜欢只接受一个参数,这就是您要传递的参数.另外,它已经为您完成了格式化.为什么不这样做呢?
Are you nesting your brackets correctly? I don't think NSLog()
likes taking only one argument, which is what you're passing it. Also, it already does the formatting for you. Why not just do this?
NSLog(@"%@ %@, %@",
errorMsgFormat,
error,
[error userInfo]);
或者,因为您说errorMsgFormat
是带有单个占位符的格式字符串,所以您要这样做吗?
Or, since you say errorMsgFormat
is a format string with a single placeholder, are you trying to do this?
NSLog(@"%@, %@", [NSString stringWithFormat:errorMsgFormat, error],
[error userInfo]);
这篇关于警告:“格式不是字符串文字,并且没有格式参数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!