我有一个数组,我正在使用它们来显示名称,但是我希望同时显示所选的当前名称和下一个名称。

IE浏览器

如果其玩家1参加比赛,则出场

玩家1走吧

玩家2准备好出发

这就是到目前为止,我只显示当前字符串并循环播放直到游戏结束。

    if (_index == _players.count) {
            _index = 0;
        }


        NSString * playerName = (NSString*)_players[_index++];
//        NSString * nextplayerName = (NSString*)_players[_index++];

        NSLog(@" player %@", playerName);

        self.turnlabel.text = playerName;


我如何显示数组中的下一项,但仍按上面的顺序继续数组?

最佳答案

你近了获取下一个玩家名称后,您不应增加_index,因为您尚未晋升至该玩家。

if (_index == _players.count)
{
  _index = 0;
}
//Get the player at the current index
NSString * playerName = (NSString*)_players[_index];

//advance the index to the next play, and "wrap around" to 0 if we are at the end.
index = (index+1) %_players.count

//load the next player's name, but don't increment _index again.
NSString *nextplayerName = (NSString*)_players[_index];

NSLog(@" player %@. nextPlayer = %@", playerName, nextplayerName);

self.turnlabel.text = playerName;

关于ios - 显示数组中的下一项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22901226/

10-11 02:16