我已经创建了创建对象的函数,但我不知道如何确保每个对象都执行该函数
-(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];
};
}
最佳答案
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];
}
}
关于objective-c - 创建的对象数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18349826/