简体   繁体   English

NSLocalizedString - 格式不是字符串文字而没有格式参数(xcode)

[英]NSLocalizedString - Format not a string literal and no format arguments (xcode)

I get the "format not a string..." message when doing the following: 执行以下操作时,我收到“format not a string ...”消息:

NSString* string1 = [[NSString alloc] initWithFormat:NSLocalizedString(@"Update Now", @"Update Now Item")];
NSString* string2 = [[NSString alloc] initWithFormat:NSLocalizedString(@"Register", @"Register Now")];

It works fine ie the app doesn't crash on the device or simulator, and the localized text displays just fine also. 它工作正常,即应用程序不会在设备或模拟器上崩溃,本地化的文本也显示正常。

I'm trying to understand why is it then that I'm getting this particular error message. 我试图理解为什么我收到这个特定的错误信息。 As far as I'm aware this is how you're meant to use localized strings in Objective C. 据我所知,这就是你在Objective C中使用本地化字符串的方式。

Simplest way to use localized string is: 使用本地化字符串的最简单方法是:

NSString* myString = NSLocalizedString(@"Update Now",@"Update Now"); NSString * myString = NSLocalizedString(@“立即更新”,@“立即更新”);

Now keep in mind myString is autoreleased - which you would generally need for a string. 现在记住myString是自动释放的 - 你通常需要一个字符串。

In the examples you give your strings are retained (because you use initWithFormat). 在示例中,您保留字符串(因为您使用initWithFormat)。 Guessing "update now" and such are going to be shown in the user UI, possibly trough a UILabel you don't need a retained string - when you do assign the string to a UILabel it will retain it automatically (as the text is stored in a retained property) 猜测“立即更新”等将在用户UI中显示,可能通过UILabel显示您不需要保留字符串 - 当您将字符串分配给UILabel时,它将自动保留它(因为文本存储在保留的财产)

NSLocalizedString is a macro, which is actually; NSLocalizedString是一个宏,实际上是;

[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]

So, why on earth do you need another NSString unless you do some post-processing like format replacing, it returns an NSString anyway! 那么,为什么你需要另一个NSString除非你做一些像格式替换这样的后处理,否则它会返回一个NSString!

you can retain it if you like... 如果你愿意,你可以保留它...

You should not use initWithFormat: here at all because you are not dealing with a format string. 你不应该使用initWithFormat:这里因为你没有处理格式字符串。

But if you use it, always use it like this: 但是如果你使用它,总是像这样使用它:

[[NSString alloc] initWithFormat:@"%@", NSLocalizedString(@"Update Now", @"Update Now Item")];

Otherwise, the risk is that the result of NSLocalizedString contains a string format specifier (like %d or something), and that would crash your app. 否则,风险是NSLocalizedString的结果包含字符串格式说明符(如%d或其他东西),这会使您的应用程序崩溃。

The warning reported by the compiler is correct. 编译器报告的警告是正确的。 The string returned from NSLocalizedString(@"Update Now", @"Update Now Item") is not a format string , because it does not have format specifier inside. NSLocalizedString(@"Update Now", @"Update Now Item") 字符串不是格式字符串 ,因为它内部没有格式说明符。

As Ican Zilb said in another answer, the best solution is to directly use: 正如Ican Zilb在另一个答案中所说,最好的解决方案是直接使用:

NSString* myString = NSLocalizedString(@"Update Now",@"Update Now Item");

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

相关问题 警告:格式不是字符串文字,也没有格式参数 - warning: format not a string literal and no format arguments 格式不是字符串文字,没有格式参数(不涉及NSLog) - format not a string literal and no format arguments (not involving NSLog) 警告消息“格式不是字符串文字,没有格式参数” - warning message “format not a string literal and no format arguments” 警告:格式不是字符串文字,也没有格式 arguments - warning: format not a string literal and no format arguments 关于字符串警告格式不是字符串文字,也没有格式参数 - About String warning format is not a string literal and no format arguments 为什么会看到错误“格式不是字符串文字且没有格式参数”? - Why am I seeing the error “Format is not a string literal and no format arguments”? DebugLog Format字符串不是字符串文字 - DebugLog Format string is not a string literal NSLocalizedString:为什么genstrings为我的格式说明符添加1 $和2 $? - NSLocalizedString: Why is genstrings adding 1$ and 2$ to my format specifiers? 新的ios4格式不是字符串文字 - New ios4 Format not a string literal 如何解决具有NSException格式的安全性问题,请使用非字符串文字 - how to address the security concern of having an NSException format use a non-string literal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM