Better way to convert NSArray of NSString to NSArray of NSNumber

1.7k views Asked by At

Is there is a better way to convert NSArray of NSString into NSArray of NSNumber than the following code:

-(NSArray*) convertStringArrayToNumberArray:(NSArray *)strings {
    NSMutableArray *numbers = [[NSMutableArray alloc] initWithCapacity:strings.count];
    for (NSString *string in strings) {
        [numbers addObject:[NSNumber numberWithInteger:[string integerValue]]];
    }
    return numbers;
}
1

There are 1 answers

0
Leo On BEST ANSWER
-(NSArray *)convertStringArrayToNumberArray:(NSArray *)strings {
    return [strings valueForKeyPath:@"self.integerValue"];
}

I test

NSArray * array = @[@"1",@"2",@"3"];
NSArray *num = [self convertStringArrayToNumberArray:array];

And result