declaring array of an object X with unknown size objective c

前端 未结 2 2126
清酒与你
清酒与你 2021-01-26 07:04

How would I go about declaring an array in the .h file of an unknown size that I will calculate say in the a function inside the class ?

For example, I might have 20 or

2条回答
  •  感情败类
    2021-01-26 07:41

    Like rmaddy says, you can just allocate the array with malloc:

    arrays_of_unknown_size= (NSArray**)malloc(N*sizeof(NSArray*));
    

    You can also reallocate it with realloc, and the size may change.
    Another way is to use an array containing an array:

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

    Then when the array is already populated, get the single array that you want:

    NSArray* myArray=[array_of_unknown_size objectAtIndex: myIndex];
    

    Of course arrays_of_unknown_size is too long, don't use this name, it's just an example.

提交回复
热议问题