23

I want to add the [NSDecimalNumber numberWithInt:i] to an array using for loop.

It is hardcoded :

 NSArray *customTickLocations = [NSArray arrayWithObjects: [NSDecimalNumber numberWithInt:1],[NSDecimalNumber numberWithInt:2],[NSDecimalNumber numberWithInt:3],[NSDecimalNumber numberWithInt:4],[NSDecimalNumber numberWithInt:5],[NSDecimalNumber numberWithInt:6],[NSDecimalNumber numberWithInt:7],[NSDecimalNumber numberWithInt:8],[NSDecimalNumber numberWithInt:9],[NSDecimalNumber numberWithInt:10],[NSDecimalNumber numberWithInt:11],[NSDecimalNumber numberWithInt:12],nil];

I want like this, but I can add only one object here....

for (int i=0; i<totalImagesOnXaxis; i++)
{
    customTickLocations = [NSArray arrayWithObject:[NSDecimalNumber numberWithInt:i]];
}

Please help me out of this, Thanks in Advance, Madan

1
  • Why do you need the array anyway? Looks like the value in the array at index i is always i. Commented Oct 21, 2011 at 8:53

7 Answers 7

45

NSArray is immutable. Use the mutable version, NSMutableArray.

Sign up to request clarification or add additional context in comments.

Comments

22
NSMutableArray * customTickLocations = [NSMutableArray new];
for (int idx = 0; idx < 12; ++idx) {
    [customTickLocations addObject:[NSDecimalNumber numberWithInt:idx]];
}

...

Comments

10

you cannot add Objects at run time to NSArray.For adding or removing objects at Run time you have to use NSMutableArray.

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

Comments

7
NSMutableArray *customTickLocations = [NSMutableArray array];
for (int i=0; i<totalImagesOnXaxis; i++)
{
    [customTickLocations addObject:[NSDecimalNumber numberWithInt:i]];
}

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray

NSMutableArray Class Reference

Comments

5

I found that using this technique is a great way to easily add a few more elements to an NSArray, this was the answer I was looking for when I came to this thread so am posting it as it is a great easy addition.

If I want to add a new array to my current array

currentArray = [currentArray arrayByAddingObjectsFromArray: newArray]; 

Comments

5
NSMutableArray *customTickLocations = [[NSMutableArray alloc] init];

for(int i = 0; i<WhateverNoYouWant;i++)
{
  NSDecimalNumber * x = [NSDecimalNumber numberWithInt:i]
  [customTickLocations addObject:x]
}

Comments

1

NSArray add object like this:

NSArray *arr = @["1","2","3","4"];

I think NSArray can not addObject like NSMutableArray. You should try it:

NSMutableArray *mulArr = [NSMutableArray new];
[mulArr addObject:[NSDecimalNumber numberWithInt:number]];

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.