“%” symbol in an NSLocalizedString
-
23-06-2021 - |
题
I have the following line of code:
NSLog([NSString stringWithFormat:NSLocalizedString(@"KEY", nil),80.1]);
The 80.1 is a float that is being inserted into the localized string. This works fine. However I want a % symbol in the localized string. I have tried using %%, but it does not output the % symbol - instead it outputs a space, the numbers 5302 and also removes part of the string that precedes the %% characters.
How can I add a % symbol to a string returned by NSLocalizedString?
解决方案
I think the problem is not with the NSLocalizedString
, but with NSLog
's interpretation of %
symbol. If you pass a format string to NSLog
, and put the string that you would like to show as an object parameter, the percentage sign %
should survive:
NSLog(@"%@", [NSString stringWithFormat:NSLocalizedString(@"KEY", nil),80.1]);
其他提示
Alternatively: The first argument for NSLog is a format string. You've got a format string, so you should pass it to NSLog:
NSLog (NSLocalizedString (@"KEY", nil), 80.1);