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
+2
A:
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
drawnonward
2010-05-25 05:45:35
Thanks. As you point out, iteration looks more attractive.
Run Loop
2010-05-25 06:01:30