tags:

views:

544

answers:

3

Hi, initWithCapacity: is declared in NSMutableArray, but I want to use it to initialize an NSArray. I there any solution?

+3  A: 

It doesn't make sense to init an NSArray using initWithCapacity:, because you can't subsequently add objects. An NSArray with no objects in has a de facto capacity of 0. What are you really trying to achieve?

Graham Lee
hai , please will you give the answer to the following urlhttp://stackoverflow.com/questions/1502749/edit-action-in-tableview-without-edit-button
senthil
That question is completely unrelated to his answer.
Brad Larson
+3  A: 

Since NSArray objects are immutable (cannot change the objects they contain) there's no use in adjusting the capacity of NSArrays.

The capacity is the number of objects an array can contain without reallocating memory. It is only used for optimization.

Nikolai Ruhe
+2  A: 

Just make an NSMutableArray with initWithCapacity:, fill it with stuff, and then make an NSArray from it.

You can use either:

NSArray *_immutableArray = [_mutableArray copy];
...
[_immutableArray release];

Or:

NSArray *_immutableArray; 
[_immutableArray initWithArray:_mutableArray];
...
[_immutableArray release];

Or:

NSArray *_immutableArray = [NSArray arrayWithArray:_mutableArray];
Alex Reynolds