Domanda

I have an array that im using to display names however I wish to show both the current name selected and also the next name.

IE

If its player 1 go at a game then show

Player 1 take your go

Player 2 get ready to take your go

This is what i have so far this only displays the current string and loops until end of game.

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


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

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

        self.turnlabel.text = playerName;

How can I display the next item in the array but still have the array continue in order as it is above?

È stato utile?

Soluzione

You're close. You should not increment _index after getting the next player name, since you are not yet advancing to that player.

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;

Altri suggerimenti

To loop over an NSArray you may want to use enumerateObjectsUsingBlock like:

[_players enumerateObjectsUsingBlock ^(id obj, NSUInteger idx, BOOL *stop){
    NSString * playerName = (NSString*)_players[_index++];
    NSLog(@" player %@", playerName);
}];

See the docs.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top