iphone - 是否可以标记 block ?

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

不幸的是,我遇到了一个问题,我无法用它的索引标记每个 block 。也就是说,到 block 执行时,i 始终为 0。有没有一种方法可以确保 block 知道启动时 i 是什么?

最佳答案

我回避了如果最后一场比赛结束我的 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];

关于iphone - 是否可以标记 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17078768/

相关文章:

iphone - Objective-C/iOS - 测试是否存在 C 函数?

ios - 应用程序大小不正确 iOS 8 iPhone 模拟器

iphone - 裁剪 UIImage 以适合框架图像

ios - 我的自定义 UIButton 无法用于替换 leftBarButtonItem

iphone - 即使应用程序终止也更新用户位置

iOS 7 在使用 UIImagePickerController 时崩溃 BAD_ACCESS

iOS:使用谷歌登录

ios - 在 swift 4 中通过短信推荐 friend

ios - 当我在 iOS 中单击谷歌上的标记时,如何用两个按钮显示覆盖信息 View

iphone - 以编程方式将内容添加到音乐库