能否对区块进行标记?

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总是为零。 - Martin R
你试过使用 __block int j=i; 来捕获 i,然后使用 j 代替 i 吗? - taffarel
3个回答

0
我绕过了这个问题,如果上一场比赛结束,我的UITableView不会重新加载,而是采取了以下措施:
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 void (^CompletionBlock)(NSInteger tag, NSData * data, NSError *err);

在定义loadMatchDataWithCompletionHandler时使用CompletionBlock。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接