Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
how to create a NSarray without initial value? i want to create an array and fill that during the running program.
 
:confused:

Code:
NSArray *myArray=[[NSArray alloc] init];

Edit to add: of course NSArray instances are immutable. If you want to add/remove objects use NSMutableArray instead of NSArray
 
If 'Attempt to mutate immutable object' is a problem, you might instead use
+ arrayWithCapacity:
– initWithCapacity:
from the NSMutableArray Class
 
If 'Attempt to mutate immutable object' is a problem, you might instead use
+ arrayWithCapacity:
– initWithCapacity:
from the NSMutableArray Class

Most mutable subclasses of immutable objects return mutable objects with the superclass method, where applicable. For instance, [NSMutableArray arrayWithArray:someArray] will return a mutable array. If a method creates an autoreleased object (class methods), it will match the class used to create it (at least in terms of mutability). If a method typically returns a different object (instance methods), it will almost always match the mutability of the (super)class that implements the method.

The linguistic rule of thumb is: if the method contains the word "With", called class mutability will be preserved; if the method contains the word "By", it will returned object will correspond to the class that implements the method (usually the immutable superclass).
 
actually what i exactly want is an array without initial value, and want to add infinite item during the running program, if i set the capacity at first that'll be possible to add extra item more than capacity later on?
 
actually what i exactly want is an array without initial value, and want to add infinite item during the running program, if i set the capacity at first that'll be possible to add extra item more than capacity later on?

You want an NSMutableArray. If you start with a capacity then exceed it, it will grow. You don't have to start with an initial capacity, but resizing is expensive.

-Lee
 
actually what i exactly want is an array without initial value, and want to add infinite item during the running program, if i set the capacity at first that'll be possible to add extra item more than capacity later on?

Excuse me, but what does the documentation in XCode say?

It is all easy to ask questions here, but do you think you should base your programming career on the help of strangers? I would suggest that you demonstrate your willingness to learn by looking up the documentation of arrayWithCapacity and posting it here, just to demonstrate to everyone here that you are not just too lazy to learn.

If you don't know how to look it up, ask about it here, because knowing how to look up documentation is hundred times more important than knowing the details of one method.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.