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

标签 ios arrays object

我有一个对象数组 _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/

相关文章:

ios - 从 NSMutableDictionary 获取所有值

ios - 当非购买用户在 iOS 中按下恢复购买按钮时会发生什么?

javascript - 如何从此 JSON 数组中删除不需要的元素?

java - 在 Static void main 中调用方法

javascript - 在 for 循环中创建新对象 - Javascript

ios - Xcode 如何使用按钮连接 View 到 View ? ( swift )

ios - 首次加载时,UIScrollView 内容位于导航栏下方

ios - 搜索时出现 TableView 错误

java - 将对象从一个数组列表移动到另一个数组列表

java - 使用 Java 反射创建原始类型/对象