如何在比赛中将数据从GKPlayer发送到另一个? [英] How do I send data from a GKPlayer to another in a match?

查看:94
本文介绍了如何在比赛中将数据从GKPlayer发送到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在比赛中将NSDataGKPlayer发送到另一个.因此,方法

I would like to send NSData from a GKPlayer to another in a match. Therefore, the method sendDataToAllPlayers(_:withDataMode:error:) would be the most ideal.

func sendDataToAllPlayers(data: Int,
    withDataMode Reliable: GKMatchSendDataMode,
    error: NSErrorPointer) -> Bool {

    return true
}

但是,如何使用上面的方法发送Int变量?

Though, how can I use the method above to send an Int variable?

var score:Int = 0

推荐答案

用于将Int(或其他类型)封装到NSData或从中提取的版本甚至应在具有不同字节顺序的设备之间工作:

This version for encapsulating an Int (or other type) into or extracting it fromNSData should work even between devices with different byte orders:

// at sender before sending data
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
archiver.encodeInteger(score, forKey: "score")
archiver.finishEncoding()

// at receiver after receiving data
let unarchiver = NSKeyedUnarchiver(forReadingWithData: data)
let score = unarchiver.decodeIntegerForKey("score")
unarchiver.finishDecoding()

这篇关于如何在比赛中将数据从GKPlayer发送到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆