How to localize all strings in an array
I have a dynamic array of strings, the elements of which I would l开发者_JS百科ike to localize. Is there a way to localize the strings without iteration e.g. something like using "makeObjectsPerformSelector". Thank you
makeObjectsPerformSelector iterates through the array. If you want to use that instead of the faster method of iterating yourself, do this:
@interface NSString (MyCategory)
-(void) localizeToArray:(NSMutableArray *)ioArray;
@end
@implementation NSString (MyCategory)
-(void) localizeToArray:(NSMutableArray *)ioArray {
[ioArray addObject:[[NSBundle mainBundle] localizedStringForKey:self value:self table:nil]];
}
@end
@interface NSArray (MyCategory)
-(NSArray *) arrayWithLocalizedStrings;
@end
@implementation NSArray (MyCategory)
-(NSArray *) arrayWithLocalizedStrings {
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]];
[self makeObjectsPerformSelector:@selector(localizeToArray:) withObject:result];
return result;
}
@end
精彩评论