创建的对象数

时间:2013-08-21 05:57:26

标签: objective-c c class object

我已创建了创建对象的函数,但我无法弄清楚如何确保每个对象都执行该函数

-(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];
    };
}

1 个答案:

答案 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];
    }
}