1

I'm trying to generate integer array using for loop. Here is my code:

int total = 20;

for(int x = 0; x < total; x++)
{
   NSArray arr = [NSArray arrayWithObjects: [NSNumber numberWithInt:total]];
}

With the above implementation I get only one object in array, the last one. What to do so arr to contain the numbers from 1 to 20 ?

6 Answers 6

2
int total = 20;

NSMutableArray *arr = [[NSMutableArray alloc] init];
for(int x = 0; x < total; x++)
{
   [arr addObject:[NSNumber numberWithInt:x]];
}

// release the array when you are done with it.
Sign up to request clarification or add additional context in comments.

3 Comments

[[NSMutableArray alloc] init]; instead of [NSMutableArray alloc] init]. Cheers
@Flex_Addicted: you know that you can edit a response from typos =) I will change it, thanx m8!
yeah, but you can edit a question only if 6 characters are modified at least. Maybe I'm wrong... Thank you anyway.
2

You should initialize your array outside of the loop rather than inside the loop.

Change your code to this:

int total = 20;
NSMutableArray *arr = [NSMutableArray array];
for(int x = 0; x < total; x++)
   [arr addObject:[NSNumber numberWithInt:x]];

Comments

1

You're recreating the array every time. Make the NSArray an iVar or initialize it outside of the loop.

Comments

1

NSArray is an immutable class (it means you cannot add/remove objects runtime) and what you are doing here is quite strange. You create a new array inside the loop.

You could use a NSMutableArray but externally to the loop. In this manner you have a reference outside the loop and you can add objects to it. NSMutableArray is mutable. You can change objects runtime.

NSMutableArray* numberArray = [[NSMutableArray alloc] init];

for(int x = 0; x < total; x++)
{
   [numberArray addObject:[NSSnumber numberWithInt:x]];
}

Comments

0
NSMutableArray *arr = [NSMutablearray arrayWithCapacity:total];
for(int x = 0; x < total; x++)
{
   [arr addObject:[NSNumber numberWithInt:x]];
}

At every loop you created new array with one object. in this example you create array once and than just add objects to it.

3 Comments

Your answer is incorrect. You should store x in the array, not total or else you'll have 20 objects all with the value of 20.
Perhaps, [NSNumber numberWithInt: i] was originally meant, but YMMV.
May want to explain what the OP was doing wrong, and why your answer solves that problem, just for completeness's sake.
0

Every time you are creating a new NSArray and assigning it to NSArray *arr. +arrayWithObjects: method create a new array. NSArray is not mutable , you should use NSMutableArray.

NSArray creates static arrays, and NSMutableArray creates dynamic arrays.

NSMuatbleArray *arr = [[NSMutableArray alloc]init];
for(int i =0 ; i<20 ; ++i)
{
[array addObject:[NSNumber numberWithInt:i]];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.