0

我已经创建了创建对象的函数,但我不知道如何确保每个对象都执行该函数

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

1 回答 1

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];
    }
}
于 2013-08-21T06:08:25.393 回答