2013-06-13 21 views
5

私はターンベースのiOSゲームに取り組んでプレイヤーが参加しているゲームの私のリストを移入しようとしています。ブロックにタグを付けることは可能ですか?

for (unsigned i = 0; i < [matches count]; i++) 
{ 
    // Only load data for games in progress. 
    // NOTE: Might want to handle finished games later. 
    if ([matches[i] status] != GKTurnBasedMatchStatusEnded) 
    { 

     // Send off another block to retrieve the match's data. 
     [(GKTurnBasedMatch*)matches[i] loadMatchDataWithCompletionHandler: ^(NSData *matchData, NSError *error) 
     { 
      // Prepare the game. 
      Game* game; 
      if (matchData.length == 0) 
      { 
       // If the match data is empty, this is a new game. Init from scratch. 
       game = [[Game alloc] init]; 
      } 
      else 
      { 
       // Otherwise, unpack the data and init from it. 
       game = [NSKeyedUnarchiver unarchiveObjectWithData:matchData]; 
      } 
      game.match = matches[i]; 

      // Load the displayNames for the players. 
      bool lastIndex = i == ([matches count] - 1); 
      [self loadPlayerIdentifiersForGame:game intoArray:blockGames lastIndex:lastIndex]; 
     }]; 
    } 
} 

残念ながら、私はそのインデックスに各ブロックにタグを付けることができない問題が生じています。つまり、iはブロックが実行されるまでに常に0です。どのブロックが起動したときにブロックがiだったかを確認できる方法はありますか?

+2

各ブロックは、ブロックが作成された時点で正確に 'i'の値を取得する必要があります。ブロックが実行されたときに 'i'が常にゼロになるべき理由がわかりません。 –

+0

あなたは私の代わりに試しましたか?__block int j = iをキャプチャしました。それから私はjを使う代わりに? – taffarel

答えて

0

私は最後の試合が終わっ代わりにこれを行うことによりだった場合、私のUITableViewはリロードしない問題をsidestepped:

GKTurnBasedMatch* match; 
for (int j = ([matches count] - 1); j >= 0; j --) 
{ 
    match = matches[j]; 
    if (match.status != GKTurnBasedMatchStatusEnded) 
     break; 
} 
bool lastIndex = (matches[i] == match); 
[self loadPlayerIdentifiersForGame:game intoArray:blockGames lastIndex:lastIndex]; 
0
-(void)someMethod { 
    ... 
    for (unsigned i = 0; i < [matches count]; i++) 
    { 
     // Only load data for games in progress. 
     // NOTE: Might want to handle finished games later. 
     if ([matches[i] status] != GKTurnBasedMatchStatusEnded) 
      [self loadMatch:i of:matches]; 
    } 
} 

-(void) loadMatch:(int)i of:(NSArray *)matches { 
    // Send off another block to retrieve the match's data. 
    [(GKTurnBasedMatch*)matches[i] loadMatchDataWithCompletionHandler: ^(NSData *matchData, NSError *error) 
    { 
     // Prepare the game. 
     Game* game; 
     if (matchData.length == 0) 
     { 
      // If the match data is empty, this is a new game. Init from scratch. 
      game = [[Game alloc] init]; 
     } 
     else 
     { 
      // Otherwise, unpack the data and init from it. 
      game = [NSKeyedUnarchiver unarchiveObjectWithData:matchData]; 
     } 
     game.match = matches[i]; 

     // Load the displayNames for the players. 
     bool lastIndex = i == ([matches count] - 1); 
     [self loadPlayerIdentifiersForGame:game intoArray:blockGames lastIndex:lastIndex]; 
    }]; 
} 
0

最も簡単な方法は、タグを含む三番目のパラメータを渡すことです。..

そして、私はより良い書き込みコードに... typedefを使用することをお勧め(およびあなたのための自動補完作業を聞かせて...)

のtypedefのボイド(^ CompletionBlock)(NSIntegerタグ、NSData *データ、NSError * err);

loadMatchDataWithCompletionHandlerを定義するときにCompletionBlockを使用します。

関連する問題