我已创建了创建对象的函数,但我无法弄清楚如何确保每个对象都执行该函数
-(id) init{
if (self = [super init]){
_die = [[NSMutableArray alloc] init];
}
return self;
}
-(void) addDice:(Die *)d{
[_die addObject:d];
}
-(void) roll{
for (int i=0; i<_die; i++) //this is the part i don't get how to do'
{
Die *d = _die[i];
[d roll];
};
}
答案 0 :(得分:0)
NSMutableArray
实例有一个方法-count
来获取当前在数组中的对象数。你可能想这样做:
-(void)roll{
for (int i=0; i<_die.count; i++) //Enumerate the array
{
Die *d = [_die objectAtIndex:i]; // Get the i th object in _die.
[d roll];
}
}