我没有NSPredicate
为基础的解决方案,或者可能是任何你想象中的优雅,但我遇到了同样的问题,并写了我自己的解决方案,并没有那么糟糕。
我的解决方案是一个游戏,只能有两个参与者,那么相应的修改,但这里是我最后使用的代码:
[myGamesArray sortUsingComparator:^NSComparisonResult(CHGame *game1,
CHGame *game2) {
if (YES == [game1 localPlayersTurn] && NO == [game2 localPlayersTurn]) {
return NSOrderedAscending;
} else if (NO == [game1 localPlayersTurn] && YES == [game2 localPlayersTurn]) {
return NSOrderedDescending;
}
NSDate *lm1 = [game1.match lastMove];
NSDate *lm2 = [game2.match lastMove];
if (lm1 != nil && lm2 != nil) {
return [lm1 compare:lm2];
}
return NSOrderedSame;
}];
其中CHGame
是一个自定义类我为我的游戏(其中具有GKTurnBasedMatch
match
属性),并且实例方法localPlayersTurn
返回BOOL
,指示它是否是本地参与者的轮到。
然后,我在一个类别写了GKTurnBasedMatch
一个lastMove
方法:
- (NSDate *)lastMove {
GKTurnBasedParticipant *localParticipant, *otherParticipant;
NSDate *lastMove;
for (GKTurnBasedParticipant *participant in self.participants) {
if (YES == [participant.playerID isEqualToString:[GKLocalPlayer localPlayer].playerID]) {
localParticipant = participant;
} else {
otherParticipant = participant;
}
}
if (localParticipant == self.currentParticipant) {
lastMove = otherParticipant.lastTurnDate;
} else {
lastMove = localParticipant.lastTurnDate;
}
return lastMove;
}
同样,这仅适用于共两个参与者,但会很容易地修改其中任意数量。
希望这会有所帮助,即使它不完全符合您的要求。
干得漂亮。我的快速创可贴解决方案是在匹配数据中包装一个'lastMove'NSDate值,并在每次转动时更新它,但是您的分类方法绝对更清晰,更简单。谢谢一堆! – cowfaboo 2013-04-01 18:17:48
请注意,如果玩家超时,这将返回错误的日期。例如。如果Player2超时,它将再次成为Player1的转向。现在,即使Player1的lastTurnDate更近,此方法也会返回Player2的lastTurnDate。 – Phlibbo 2017-02-07 19:19:04