我有一个动态的字符串数组,我想要本地化的元素。有没有办法在没有迭代的情况下本地化字符串,例如比如使用“makeObjectsPerformSelector”。谢谢
答案 0 :(得分:2)
makeObjectsPerformSelector遍历数组。如果您想使用它而不是更快的迭代方法,请执行以下操作:
@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