fatal error: can't unsafeBitCast between types of different sizes (using gamekit)
fatal error: can't unsafeBitCast between types of different sizes (using gamekit)
使用此处找到的 GameKit 多人游戏功能 (EasyGameCenter):https://github.com/DaRkD0G/Easy-Game-Center-Swift
当两个玩家连接时,我在这条线上崩溃了
let playerIDs = match.players.map { [=10=] .playerID } as! [String]
在控制台中使用这个
fatal error: can't unsafeBitCast between types of different sizes
有什么想法吗?这里是完整的功能,方便参考:
@available(iOS 8.0, *)
private func lookupPlayers() {
guard let match = EGC.sharedInstance.match else {
EGC.printLogEGC("No Match")
return
}
let playerIDs = match.players.map { [=11=] .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?()
}
}
问题是您的 map
语句导致 Array<String?>
类型,因为 playerID
是 String?
,您不能将其直接转换为 Array<String>
.
如果您确定您将始终拥有一个 playerID
值,您可以更改语句
match.players.map { [=10=].playerID }
至:
match.players.map { [=11=].playerID! }
如果您不确定这一点,那么您可以使用 Array<String?>
值并进行适当的可选处理,或者通过从 map
切换到 [来删除 nil
值=22=]:
match.players.flatMap { [=12=].playerID }
使用此处找到的 GameKit 多人游戏功能 (EasyGameCenter):https://github.com/DaRkD0G/Easy-Game-Center-Swift
当两个玩家连接时,我在这条线上崩溃了
let playerIDs = match.players.map { [=10=] .playerID } as! [String]
在控制台中使用这个
fatal error: can't unsafeBitCast between types of different sizes
有什么想法吗?这里是完整的功能,方便参考:
@available(iOS 8.0, *)
private func lookupPlayers() {
guard let match = EGC.sharedInstance.match else {
EGC.printLogEGC("No Match")
return
}
let playerIDs = match.players.map { [=11=] .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?()
}
}
问题是您的 map
语句导致 Array<String?>
类型,因为 playerID
是 String?
,您不能将其直接转换为 Array<String>
.
如果您确定您将始终拥有一个 playerID
值,您可以更改语句
match.players.map { [=10=].playerID }
至:
match.players.map { [=11=].playerID! }
如果您不确定这一点,那么您可以使用 Array<String?>
值并进行适当的可选处理,或者通过从 map
切换到 [来删除 nil
值=22=]:
match.players.flatMap { [=12=].playerID }