声明对象X的大小未知的对象X的数组 [英] declaring array of an object X with unknown size objective c
问题描述
我该如何在.h文件中声明一个未知大小的数组,该数组将在类内的a函数中计算出来?
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 ?
例如,我可能有20或30个NSArray(只是一个例子,不是我所需要的),但是当第一次调用该类时,我不知道确切的数字
For example, I might have 20 or 30 NSArrays (just an example, not what I need), but I won't know the exact number when the class is first called
在实现文件中..
-(id) init {
if self = ..
number_of_arrays = 50; // this can be whatever value
}
在.h中:
int number_of_arrays;
NSArray *arrays_of_unknown_size[number_of_arrays]; // but number of arrays is not init !
此外,NSArray ** arrays的意义是什么?我可以在h文件中声明该文件,然后在.m文件中声明其实际大小吗?
Also, what is the significance of NSArray **arrays ? Would I be able to declare that in the h file, and then in the .m file, declare the actual size ?
谢谢!
推荐答案
您有两种选择.
1)将其设为NSArrays的NSArray,或者
1) Make it an NSArray of NSArrays, or
2)声明为:
NSArray **arrays_of_unknown_size;
然后,当您知道实际大小时就可以malloc/calloc指针.然后,您需要为C数组的每个元素分配一个NSArray.
Then you can malloc/calloc the pointer when you know the actual size. Then you need to assign an NSArray to each element of the C-array.
这篇关于声明对象X的大小未知的对象X的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!