致命错误:不能在不同大小的类型之间使用unsafeBitCast(使用游戏包) [英] fatal error: can't unsafeBitCast between types of different sizes (using gamekit)
问题描述
在此处使用GameKit多人游戏功能(EasyGameCenter): https://github.com/DaRkD0G/简易游戏中心-Swift
Using GameKit Multiplayer feature (EasyGameCenter) found here: https://github.com/DaRkD0G/Easy-Game-Center-Swift
在连接两个玩家时,我在这条线上撞车了
Upon two players connecting I get a crash on this line
let playerIDs = match.players.map { $0 .playerID } as! [String]
在控制台中使用此
严重错误:不能在不同大小的类型之间使用unsafeBitCast
fatal error: can't unsafeBitCast between types of different sizes
有什么想法吗?以下是功能齐全,供您参考:
Any ideas? Here is full function for easy reference:
@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?()
}
}
推荐答案
问题是您的map
语句生成的类型为Array<String?>
,因为playerID
是String?
,您不能直接投射到Array<String>
.
The problem is that your map
statement is resulting in a type of Array<String?>
because playerID
is a String?
, which you can't cast directly to Array<String>
.
如果确定您将始终具有playerID
值,则可以更改该语句
If you're certain you will always have a playerID
value, you could change the statement
match.players.map { $0.playerID }
收件人:
match.players.map { $0.playerID! }
如果不确定,可以使用Array<String?>
值进行适当的可选处理,也可以通过从map
切换到flatMap
去除nil
值:
If you're not certain of that, then you can either use the Array<String?>
value with appropriate optional handling or strip out the nil
values by switching from map
to flatMap
:
match.players.flatMap { $0.playerID }
这篇关于致命错误:不能在不同大小的类型之间使用unsafeBitCast(使用游戏包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!