Madan Mohan
Madan Mohan

Reputation: 8810

How to add objects to an NSArray using for loop?

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

Upvotes: 23

Views: 74648

Answers (7)

Radu
Radu

Reputation: 3494

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

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

Upvotes: 5

AmyNguyen
AmyNguyen

Reputation: 436

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

Upvotes: 1

sam_smith
sam_smith

Reputation: 6093

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

Upvotes: 5

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

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]];
}

Upvotes: 10

EmptyStack
EmptyStack

Reputation: 51374

NSArray is immutable. Use the mutable version, NSMutableArray.

Upvotes: 45

beryllium
beryllium

Reputation: 29767

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

Upvotes: 7

justin
justin

Reputation: 104698

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

...

Upvotes: 22

Related Questions