2013-06-13 5 views
5

Ich arbeite an einem rundenbasierten iOS-Spiel und versuche, meine Liste der Spiele, die Spieler in. LeiderIst es möglich, Blöcke zu markieren?

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

beteiligt zu füllen, ich ein Problem habe, wo ich nicht jeden Block mit seinem Index markieren kann . Das heißt, i ist immer 0, wenn der Block ausgeführt wird. Gibt es eine Möglichkeit, dass ich sicherstellen kann, dass der Block weiß, was i WAS zum Zeitpunkt seiner Einführung war?

+2

Jeder Block sollte genau den Wert von "i" zum Zeitpunkt der Erstellung des Blocks erfassen. Ich kann nicht sehen, warum "i" immer Null sein sollte, wenn der Block ausgeführt wird. –

+0

hast du statt i versucht, den __block int j = i zu erfassen; und dann benutze ich statt j? – taffarel

Antwort

0

wich ich das Problem, dass meine UITableView nicht nachladen, wenn das letzte Spiel, indem Sie diese stattdessen ist vorbei:

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

der einfachste Weg ist, einen dritten Parameter den Tag zu übergeben enthält ..

und ich schlage vor typedef zu verwenden .. besser Code schreiben (und lassen Sie die automatische Vervollständigung der Arbeit ..)

typedef void (^ CompletionBlock) (NSInteger Tag , NSData * -Daten, NSError * err);

und verwenden Sie CompletionBlock beim Definieren von loadMatchDataWithCompletionHandler.

Verwandte Themen