tags:

views:

1933

answers:

1

Hi,

I have to create a dynamic NSArray. That is I dont know the size of the array or what are all the elements the array is going to have. the elements need to be added to the array dynamically. I looked at the NSArray class reference. there is a method called arrayWithObejcts, which should be used at the time of initializing the array itself. But i dont know how to achieve what I need to do. please help me.

I need to do some thing like the following:

NSArray *stringArray = [[NSArray init] alloc] ;
for(int i=0; i< data.size; i++){
stringArray.at(i) = getData(i); }

+4  A: 

If you create an NSArray you won't be able to add elements to it, since it's inmutable. You should try using NSMutableArray instead.

Also, you inverted the order of alloc and init. Alloc creates an instance and init initializes it.

The code would look something like this (assumming getData is a global function):

NSMutableArray *stringArray = [[NSMutableArray alloc] init];
for(int i=0; i< data.size; i++){
   [stringArray addObject:getData(i)];
}
pgb
Thanks a lot. I wasted a lot of time without posting question in this forum.
saikamesh