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

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Madan Mohan
  • 8,764
  • 17
  • 62
  • 96
  • Why do you need the array anyway? Looks like the value in the array at index `i` is always `i`. – raymi Oct 21 '11 at 08:53

7 Answers7

46

NSArray is immutable. Use the mutable version, NSMutableArray.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
22
NSMutableArray * customTickLocations = [NSMutableArray new];
for (int idx = 0; idx < 12; ++idx) {
    [customTickLocations addObject:[NSDecimalNumber numberWithInt:idx]];
}

...
justin
  • 104,054
  • 14
  • 179
  • 226
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]];
}
Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90
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

beryllium
  • 29,669
  • 15
  • 106
  • 125
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]; 
sam_smith
  • 6,023
  • 3
  • 43
  • 60
5
NSMutableArray *customTickLocations = [[NSMutableArray alloc] init];

for(int i = 0; i<WhateverNoYouWant;i++)
{
  NSDecimalNumber * x = [NSDecimalNumber numberWithInt:i]
  [customTickLocations addObject:x]
}
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Radu
  • 3,434
  • 4
  • 27
  • 38
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]];
AmyNguyen
  • 437
  • 6
  • 10