Reputation: 20376
I have a dynamic array of strings, the elements of which I would like to localize. Is there a way to localize the strings without iteration e.g. something like using "makeObjectsPerformSelector". Thank you
Upvotes: 1
Views: 1168
Reputation: 53669
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
Upvotes: 2