Iphone 创建数量未知的对象
我需要基于变量创建特定数量的对象实例。所以伪代码看起来有点像这样Iphone 创建数量未知的对象,iphone,objective-c,object,for-loop,Iphone,Objective C,Object,For Loop,我需要基于变量创建特定数量的对象实例。所以伪代码看起来有点像这样 for(int x; x < aInt; x++) { //create object and initialize it } (int x;x
for(int x; x < aInt; x++) {
//create object and initialize it
}
(int x;x{
//创建对象并初始化它
}
如何做到这一点并每次使用不同的名称和内存位置创建不同的对象?将引用粘贴在
NSMutableArray
(或者NSArray
,前提是您似乎事先知道其大小)
NSMutableArray*array=[[NSMutableArray alloc]init]
对于(int x;x
NSDictionary
/NSMutableDictionary
当然也是一个选项,这取决于您的需求。只需使用:
NSMutableArray*objectsArray=[NSMutableArray arrayWithCapacity:(YourIntegerValue)];
对于(int x;x
使用完阵列后,不要忘记释放它
NSMutableArray *array = [[NSMutableArray alloc]init]
for(int x; x < aInt; x++) {
//create object and initialize it
YourObject *o = [[YourObject alloc]init];
[array addObject:o];
[o release];
}
// do whatever you need to do with the objects
NSMutableArray *objectsArray = [NSMutableArray arrayWithCapacity:(YourIntegerValue)];
for(int x; x < aInt; x++) {
//create object and initialize it
[objectsArray addObject:(YourObject)];
}