使用GameKit多人游戏功能(EasyGameCenter):https://github.com/DaRkD0G/Easy-Game-Center-Swift
两名球员连接后,我在这条线上发生了撞车事件
let playerIDs = match.players.map { $0 .playerID } as! [String]
在控制台中使用
致命错误:不能在不同大小的类型之间使用unsafeBitCast
有什么想法吗?这是完整的功能,以便于参考:
@available(iOS 8.0, *)
private func lookupPlayers() {
guard let match = EGC.sharedInstance.match else {
EGC.printLogEGC("No Match")
return
}
let playerIDs = match.players.map { $0 .playerID } as! [String]
/* Load an array of player */
GKPlayer.loadPlayersForIdentifiers(playerIDs) {
(players, error) in
guard error == nil else {
EGC.printLogEGC("Error retrieving player info: \(error!.localizedDescription)")
EGC.disconnectMatch()
return
}
guard let players = players else {
EGC.printLogEGC("Error retrieving players; returned nil")
return
}
if EGC.debugMode {
for player in players {
EGC.printLogEGC("Found player: \(player.alias)")
}
}
if let arrayPlayers = players as [GKPlayer]? { self.playersInMatch = Set(arrayPlayers) }
GKMatchmaker.sharedMatchmaker().finishMatchmakingForMatch(match)
(Static.delegate as? EGCDelegate)?.EGCMatchStarted?()
}
}
答案 0 :(得分:5)
问题是,您的map
语句导致Array<String?>
类型,因为playerID
是String?
,您无法直接投放到Array<String>
}}
如果您确定您将始终拥有playerID
值,则可以更改语句
match.players.map { $0.playerID }
为:
match.players.map { $0.playerID! }
如果您不确定,那么您可以使用Array<String?>
值进行适当的可选处理,也可以通过从nil
切换到{{1来删除map
值}}:
flatMap