致命错误:不能在不同大小的类型之间使用unsafeBitCast(使用gamekit)

时间:2015-11-05 17:17:13

标签: swift game-center gamekit

使用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?()

        }
    }

1 个答案:

答案 0 :(得分:5)

问题是,您的map语句导致Array<String?>类型,因为playerIDString?,您无法直接投放到Array<String> }}

如果您确定您将始终拥有playerID值,则可以更改语句

match.players.map { $0.playerID }

为:

match.players.map { $0.playerID! }

如果您不确定,那么您可以使用Array<String?>值进行适当的可选处理,也可以通过从nil切换到{{1来删除map值}}:

flatMap