我在做游戏中心的游戏。 我发送包含一个数据包struct MovePacket
是这样的:
var packet = MovePacket(packetID: 1, move: myMove)
let dataPacket = NSData(bytes: &packet, length: sizeof(MovePacket))
currentMatch!.sendDataToAllPlayers(dataPacket, withDataMode: GKMatchSendDataMode.Reliable, error: nil)
这就像在Objective-C,但我不知道如何接收解码NSData
回我的struct MovePacket
。 这是我的尝试中的一种:
func match(match: GKMatch!, didReceiveData data: NSData!, fromRemotePlayer player: GKPlayer!) {
var packet : MovePacket
data.getBytes(&packet, length: sizeof(MovePacket)) // getting error here: Address of variable 'packet' taken before it is initialized
println(packet) //Variable 'packet' used before being initialized
}