0

I have an NSArray of strings, like so:

NSString *numbers = "1 2 3 4 5 6 7 8 9 10";
NSArray *numberArray = [numbers componentsSeperatedByString:@" "];

I want to convert this entire array of string numbers into ints. How can I do so?

4

1 回答 1

2

您不能将int's 数组(它们是原始 C 类型)直接存储在 NSArray(需要 Objective C 对象)中,您需要将这些“ int”值填充到“ NSNumber”对象中。

您可以通过以下方式执行此操作:

NSMutableArray * newNumberArray = [[NSMutableArray alloc] initWithCapacity: [numberArray count]];
for(NSString * numberString in numberArray)
{
    NSNumber * trueNumber = [NSNumber numberWithInteger: [numberString integerValue]];
    [newNumberArray addObject: trueNumber];
}
于 2013-08-25T19:44:47.227 回答