views:

48

answers:

1

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
Thanks. As you point out, iteration looks more attractive.
Run Loop