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 i can do it.how can i implement in a for loop.Is there any other way to hold numbers in array in iphone.Please help me out.Thanks.

Answers


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);

Need Your Help

How do I add a submodule to a sub-directory?

git git-submodules

I have a git repo in ~/.janus/ with a bunch of submodules in it. I want to add a submodule in ~/.janus/snipmate-snippets/snippets/, but when I run git submodule add &lt;git@github.com:...&gt; in the

What is the difference between a Cluster and MPP supercomputer architecture?

cluster-computing supercomputers

What is the difference between a Cluster and MPP supercomputer architecture?