Objective c 如何使用新的for循环
我想在目标c中使用新的for循环 比如说Objective c 如何使用新的for循环,objective-c,loops,new-operator,Objective C,Loops,New Operator,我想在目标c中使用新的for循环 比如说 NSMutablearray *array = [[NSMutableArray alloc]] init]; for (int i=0 ; i< 5; i++){ [array addObject:[[Direction alloc] initWith:random()]; } NSMutablearray*array=[[NSMutablearray alloc]]init]; 对于(int i=0;i
NSMutablearray *array = [[NSMutableArray alloc]] init];
for (int i=0 ; i< 5; i++){
[array addObject:[[Direction alloc] initWith:random()];
}
NSMutablearray*array=[[NSMutablearray alloc]]init];
对于(int i=0;i<5;i++){
[array addObject:[[Direction alloc]initWith:random();
}
当我运行它时,只有一个对象,然后用消息停止。
但是
NSMutablearray*array=[[NSMutablearray alloc]]init];
方向*d=[[Direction alloc]initWith:random()]
对于(int i=0;i<5;i++){
[数组addObject:d];
[数组addObject:d];
[数组addObject:d];
[数组addObject:d];
[数组addObject:d];
}
没有错误。NSMutableArray*数组=[[[NSMutableArray alloc]]init]autorelease];
NSMutableArray *array = [[[NSMutableArray alloc]] init] autorelease];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (int i=0 ; i< 5; ++i){
Direction *direction = [[[Direction alloc] initWith:(arc4random()%100)] autorelease];
[array addObject:direction];
}
[pool drain];
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
对于(int i=0;i<5;++i){
方向*方向=[[Direction alloc]initWith:(arc4random()%100)]自动释放];
[数组addObject:方向];
}
[泳池排水沟];
它给出错误,因为您的方向对象未释放。你可以用两种方法来分配每一个时间循环的值
Direction * d= [[Direction alloc];
For (loop)
{
d=random() // something like this
}
或
上面的人所说的你可以自动恢复
Direction * d= [[Direction alloc];
For (loop)
{
d=random() // something like this
}