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