I have a tiny question regarding NSStrings. I'm currently making an app, the development language is English. I have a string like this:

NSLocalizedString(@"cancel", @"cancel string");

I have intentions of localising this app to Spanish afterwards so I need to do that. Now, the problem I have is that I need to compare this specific string to another string. So suppose I have this:

NSString *cancelString = NSLocalizedString(@"cancel", @"cancel string");

Can I just compare this string to the other one like this?:

[cancelString isEqualToString:@"cancel"];

And it will work fine? Or what will happen if the user is using my app in Spanish (so cancelString says "cancelar" instead of "cancel")? Does equalToString only compare to the base language of the app?

Hope I'm being clear.

有帮助吗?

解决方案

Despite the fact that there's probably a better way to achieve the comparison you're trying to do here, you should compare against NSLocalizedString(@"cancel", @"whatever") because that will end up changing after the preprocessor runs and your 'isEqualToString' will only compare the two results of the methods (string against string).

NSString *cancelString = NSLocalizedString(@"cancel", @"cancel string");

[cancelString isEqualToString:NSLocalizedString(@"cancel", @"cancel string")];

should work

其他提示

You could try localizedCompare

NSString *cancelString = NSLocalizedString(@"cancel", @"cancel string");
bool areEqual = ([cancelString localizedCompare:@"cancel"] == NSOrderedSame);

Reference: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/localizedCompare:

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top