Jaume
Jaume

Reputation: 923

initialise an NSArray with an unknown size

Once an Array is initialized, in order to set value of desired position, I am using

[self.appName replaceObjectAtIndex:x withObject:[self.appCell objectAtIndex: 0]];

Problem is that if I initialize appName array without objects, this array keeps empty and I must initialize it using initWithObjects and then works. Problem is that I do not know the size of an array and if I set it like:

NSMutableArray *nameArray = [[ NSMutableArray alloc] initWithObjects:@"test",@"test",@"test",@"test", nil];
self.appName = nameArray;
[nameArray release];

For example, works from 0 to 3 but from position 4 to following ones, after replaceObjectAtIndex, position has a nil value. How to solve it? Thank you

Upvotes: 0

Views: 963

Answers (2)

jrturton
jrturton

Reputation: 119292

It's a mutable array, you can add objects (to the end) and insert objects (in the middle) as well, whenever you like:

NSMutableArray *nameArray = [[NSMutableArray alloc] init];
[nameArray addObject:@"test"];
[nameArray insertObject:@"another test" atIndex:0];
[nameArray removeObjectAtIndex:1];

You can do this anywhere you have a pointer to the mutable array.

Upvotes: 1

mmmmmm
mmmmmm

Reputation: 32720

Look at the count methon on NSArray it gives you the number of elements in the array. So check that your index x is less than the count then you can remove the element.

Upvotes: 0

Related Questions