2014-04-06 7 views
0

이름을 표시하는 데 사용하는 배열이 있지만 선택한 현재 이름과 다음 이름을 모두 표시하고자하는 배열이 있습니다.배열의 다음 항목 표시

IE

게임에서의 플레이어 1 이동 한 다음이 내가 지금 무엇을 당신의 이동

플레이어 2을

플레이어 1을 표시하여 이동

을 준비하는 경우 지금까지는 게임이 끝날 때까지 현재 문자열과 루프 만 표시합니다.

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


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

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

     self.turnlabel.text = playerName; 

배열의 다음 항목을 표시 할 수 있지만 위와 같이 배열을 계속 유지하려면 어떻게해야합니까?

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

docs를 참조하십시오

+0

'_index + 1 % _players.count'을 사용해 보셨습니까? (인덱스와 하나의 모듈 어레이 수) – BergQuester

+0

어떻게 사용합니까? – user3502233

답변

2

끝났습니다. 다음 플레이어 이름을 얻은 후에 _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; 
+0

안녕하세요. 그러나 두 위치에서 오류가 발생하는 것은 index = (index + 1) % _players.count의 %가 잘못된 피연산자를 오류에 제공하고 있습니다. 또한 nslog에서 nextplayersname을 인정하지 않습니다. 어떤 아이디어? – user3502233

+0

index = (index + 1) % _players.count; 이 줄은 저에게 오류를주고 있습니다 – user3502233

+0

_index = (_index + 1) % _players.count; 인덱스 앞에 _가 필요하다는 것을 고정시키고; 끝내 미안하지만 몇 번이나 내 눈을 문질러 야 했어. :) – user3502233

0

은 NSArray를 반복 할 경우 같은 enumerateObjectsUsingBlock를 사용할 수 있습니다.

+0

마지막 반복에서 충돌이 발생합니다. – vikingosegundo