2011-08-12 56 views
1

我需要基于变量创建对象的特定数字实例。这样的伪代码看起来有点像这样创建未知数量的对象

for(int x; x < aInt; x++) { 
    //create object and initialize it 
} 

我将如何做到这一点,创建一个不同的对象有不同的名称和存储位置每次都?

回答

2

将参考文献粘贴在NSMutableArray(或NSArray,因为您似乎事先知道尺寸)。

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 

一个NSDictionary/NSMutableDictionary肯定是一个选项,以及,根据您的要求是什么。

0

只需使用一个NSMutableArray:你完成了它的工作后

NSMutableArray *objectsArray = [NSMutableArray arrayWithCapacity:(YourIntegerValue)]; 
for(int x; x < aInt; x++) { 
    //create object and initialize it 
    [objectsArray addObject:(YourObject)]; 
} 

不要忘记发布数组!