2013-06-13 46 views
5

Tôi đang làm việc trên một lần lượt dựa iOS trò chơi và cố gắng để cư danh sách các trò chơi người chơi sẽ được tham gia.Có thể gắn thẻ các khối không?

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

Thật không may, tôi đang gặp một vấn đề mà tôi không thể tag mỗi khối với chỉ số của nó . Tức là, i luôn là 0 vào thời điểm khối thực hiện. Có cách nào để tôi có thể chắc chắn rằng khối này biết những gì i WS vào thời điểm nó được khởi chạy?

+2

Mỗi khối sẽ nắm bắt chính xác giá trị của 'i' tại thời điểm khối được tạo. Tôi không thể thấy tại sao 'i' phải luôn bằng 0 khi khối được thực hiện. –

+0

bạn đã thử thay vì i, nắm bắt __block int j = i; và sau đó thay vì tôi sử dụng j? – taffarel

Trả lời

0

Tôi tránh né những vấn đề mà UITableView tôi sẽ không tải lại nếu trận đấu cuối cùng đã kết thúc bằng cách làm này để thay thế:

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

cách đơn giản nhất là phải vượt qua một tham số thứ ba chứa các thẻ ..

và tôi đề nghị sử dụng typedef .. để viết mã tốt hơn (và để làm việc autocompletion cho bạn ..)

typedef void (^ CompletionBlock) (NSInteger tag , Dữ liệu NSData *, NSError * err);

và sử dụng CompletionBlock khi xác định loadMatchDataWithCompletionHandler.

Các vấn đề liên quan