Best way to convert NSArray from NSString to NSArray from NSNumber
Is there a better way to convert NSArray
from NSString
to 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;
}
+3
Mahmoud Adam
source
to share
1 answer
-(NSArray *)convertStringArrayToNumberArray:(NSArray *)strings {
return [strings valueForKeyPath:@"self.integerValue"];
}
I am testing
NSArray * array = @[@"1",@"2",@"3"];
NSArray *num = [self convertStringArrayToNumberArray:array];
And the result
+13
Leo
source
to share