使用整数值实例化NSArray

时间:2015-05-06 12:18:10

标签: ios objective-c nsarray

我有一个PickerView,我想用整数值初始化它。例如PickerView,所有整数都小于10。

谢谢

4 个答案:

答案 0 :(得分:3)

NSArray *array =[NSArray arrayWithObjects:@1,@2,@3,@4,@5,@6,@7,@8,@9,nil];

答案 1 :(得分:2)

NSMutableArray *yourArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 10; i++) {
        [yourArray addObject:[NSNumber numberWithInt:i]];
    }

答案 2 :(得分:0)

NSInteger min = 1;
NSInteger max = 10;
NSInteger step = 1;

NSMutableArray<NSNumber *> *options = [NSMutableArray new];

for (NSInteger index = 0; index < ceilf(((float)max)/step); index++)
{
    options[index] = @((index + min) * step);
}

NSLog(@"%@",options);    

另一种方法:

NSInteger from = 1;
NSInteger size = 10;
NSInteger step = 1;

NSMutableArray<NSNumber *> *options = [NSMutableArray new];

for (NSInteger index = 0; index < size; index++)
{
    options[index] = @((index + from) * step);
}

NSLog(@"%@",options);

以上两点都打印了以下内容:

(     1,     2,     3,     4,     5,     6,     7,     8,     9,     10 )

答案 3 :(得分:-2)

int yourInt = 5;
[myMutableArray addObject:[NSNumber numberWithInt:yourInt]]; have look on this 

它只是示例代码将int值添加到数组中