我有一个对象数组 _schedule.games
我想在循环播放时间表时显示每个游戏中的 Game 属性对手。
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.
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;
}
关于ios - 如何访问对象数组中对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18306863/