开发者

How to add numbers in a mutable array in iPhone?

I am new to iPhone development. I want a Nsmutable array to hold numbers from 1 to 100. How can I do it? How can I imple开发者_开发技巧ment in a for loop? Is there any other way to hold numbers in array in iPhone?


You can only add NSObject subclasses in Cocoa containers. In your case, you will have to wrap your integers in NSNumber objects:

NSMutableArray *array = [NSMutableArray array];
for( int i = 0; i < 100; ++i )
{
   [array addObject:[NSNumber numberWithInt:i]];
}

To extract the values:

int firstValue = [[array objectAtIndex:0] intValue];


Use an NSNumber object:

[NSNumber numberWithInt:1];


The short hand solution

NSMutableArray *array = [NSMutableArray array];
for( int i = 0; i < 100; ++i )
{
   [array addObject:@(i)];
}


int intValue = 10;
NSNumber *numberObj  = @(intValue);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜