user2553675
user2553675

Reputation: 535

How to create NSMutableArray of unknown capacity?

I want to create an array (NSMutableArray) of unknown capacity and to add elements to this array one by one and delete some of them soon, so its capacity could be changed. Is it possible with NSMutableArray class?

Upvotes: 0

Views: 155

Answers (4)

Greg
Greg

Reputation: 25459

You don't have to specify capacity of the array you can create it like that:

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

You can keep adding and removing the object from it without any problem.

I assume that this is confuse by you:

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:20];

It create array with capacity for 20 object but you can easily add more without redelcaring that array.

Upvotes: 2

Thilina Chamath Hewagama
Thilina Chamath Hewagama

Reputation: 9040

Yes, you can do that. You don't have to specify array size when you create it. There are no limitations for the number of items in NSMutableArray.

Upvotes: 0

Larme
Larme

Reputation: 26096

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

When you want to add an object:

[myMutableArray addObject:theObject];

Upvotes: 0

Hackmodford
Hackmodford

Reputation: 3970

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

or

NSMutableArray *newArray = [NSMutableArray array];

Upvotes: 1

Related Questions