是否可以标记块?

时间:2013-06-13 03:33:57

标签: iphone ios objective-c ios6 objective-c-blocks

我正在开发一款基于回合制的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是什么?

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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];
     }];
}

答案 2 :(得分:0)

最简单的方法是传递包含标记的第三个参数..

我建议使用typedef ..来更好地编写代码(让自动完成功能为你工作..)

typedef void(^ CompletionBlock)(NSInteger标记,NSData *数据,NSError *错误);

并在定义loadMatchDataWithCompletionHandler时使用CompletionBlock。