如何访问对象数组中对象的属性

时间:2013-08-19 05:25:24

标签: ios arrays object

我有一个对象数组_schedule.games我希望在循环计划时显示每个游戏中的游戏属性对手。

    int x = 0;
    for (int i = 0; i < [_schedule.games count]; i++)
    {
        Game *game = [_schedule.games objectAtIndex:i];
        game.opponent = ((Game *) [_schedule.games objectAtIndex:i]).opponent;
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, 100)];

        [button setTitle:[NSString stringWithFormat:@"%@", game.opponent] forState:UIControlStateNormal];

        [_gameScrollList addSubview:button];

        x += button.frame.size.width;

    }

1 个答案:

答案 0 :(得分:1)

1

    Game *game = [_schedule.games objectAtIndex:i];

为您提供数组内的游戏实例,因此无需再次将属性指定为

game.opponent = ((Game *) [_schedule.games objectAtIndex:i]).opponent;

game.opponent具有数组对象属性中的值,因此您可以直接将其称为game.opponent

2

[NSString stringWithFormat:@"%@", game.opponent]game.opponent是一个字符串,因此无需再次将其作为NSString进行类型转换

所以方法将是

int x = 0;
for (int i = 0; i < [_schedule.games count]; i++)
{
    Game *game = (Game *)[_schedule.games objectAtIndex:i];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, 100)];
    [button setTitle:game.opponent forState:UIControlStateNormal];
    [_gameScrollList addSubview:button];
    x += button.frame.size.width;
}