Pinku
Pinku

Reputation: 71

How to store integer value into an NSMutableArray in iphone

I have NSMutableArray having values 10,15,26,28. I want to store them into another array as an integer form how do i store them. Thanks :)

Upvotes: 3

Views: 5729

Answers (4)

barryjones
barryjones

Reputation: 2309

Here's an example of how to do this:

int yourInt = 5;
[myMutableArray addObject:[NSNumber numberWithInt:yourInt]];

Upvotes: 2

gsempe
gsempe

Reputation: 5499

If you want to store integers in an array it has to be a C array:

#define C_ARRAY_MAX_SIZE 10
int cTab[C_ARRAY_MAX_SIZE];
int i=0;

for (NSNumber* n in yourMutableArray) {


 cTab[i] = [n intValue];
    i++;

}

Upvotes: 0

Sascha
Sascha

Reputation: 5973

You can't store C types in a NSMutableArray, you can only store objects. Create NSNumber's from your int values with [NSNumber numberWithInteger:10];...

You can then get the int value back with [aNSNumberObject intValue];

Upvotes: 0

Rahul Vyas
Rahul Vyas

Reputation: 28720

here how to add

[array addObject:[NSNumber numberWithInt:10]];

you can get the integer value like

//assuming array has nsnumber/nsstring objects.

[[array objectAtIndex:index] intValue];

Upvotes: 11

Related Questions