2013-06-13 9 views
5

pracuję nad turowa gra iOS i stara się wypełnić moją listę gier gracz uczestniczy w.Czy można oznaczyć bloki?

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]; 
     }]; 
    } 
} 

Niestety mam problem, gdzie nie mogę oznaczyć każdy blok z indeksem . To znaczy, i jest zawsze 0 przez czas wykonania bloku. Czy jest jakiś sposób, aby upewnić się, że blok wie, co było w momencie jego uruchomienia?

+2

Każdy blok powinien przechwytywać dokładnie wartość "i" w momencie tworzenia bloku. Nie widzę powodu, dla którego 'i' powinno zawsze wynosić zero, gdy blok jest wykonywany. –

+0

czy próbowałeś zamiast i, przechwycisz __block int j = i; a następnie zamiast używać j? – taffarel

Odpowiedz

0

I pominął kwestię gdzie mój UITableView nie przeładować jeżeli ostatnia gra dobiegła końca w ten sposób zamiast:

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

Najprostszym sposobem jest zdać trzeci parametr zawierający tag ..

i sugeruję użycie typedef .. do lepszego kodu zapisu (i niech pracę autouzupełniania dla ciebie ..)

typedef void (^ CompletionBlock) (tag NSInteger , NSData * dane, NSError * err);

i użyj bloku CompletionBlock podczas definiowania loadMatchDataWithCompletionHandler.