How do I send data from a GKPlayer to another in a match?
Asked Answered
P

2

0

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
}

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

var score:Int = 0
Pederson answered 22/2, 2015 at 20:20 Comment(5)
Do you need to convert Int to NSData and Back to int ?Georas
I probably do. It would be the better solution for my case. Any help is much appreciated. Thanks!Pederson
raywenderlich.com/3276/…Georas
www1.in.tum.de/lehrstuhl_1/home/98-teaching/tutorials/…Georas
github.com/jackcook/GCHelper/blob/master/Source/GCHelper.swiftGeoras
E
2

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()
Ecosphere answered 25/2, 2015 at 18:56 Comment(0)
G
3
extension Int {
    var data: NSData {
        var source = self
        return NSData(bytes: &source, length: sizeof(Int))
    }
}
extension Double {
    var data: NSData {
        var source = self
        return NSData(bytes: &source, length: sizeof(Double))
    }
}
extension NSData {
    var integerValue:Int {
        var result = 0
        getBytes(&result, length: sizeof(Int))
        return result
    }
    var doubleValue:Double {
        var result:Double = 0
        getBytes(&result, length: sizeof(Double))
        return result
    }
}

let myIntegerData = 123.data                 // _NSInlineData
let backToInt = myIntegerData.integerValue   // 123

let myDoubleData = 123.456789.data           // _NSInlineData
let backToDouble = myDoubleData.doubleValue  // 123.456789
Georas answered 22/2, 2015 at 20:26 Comment(4)
Thank you! How would I use this code in the sendDataToAllPlayers(_:withDataMode:error:) method? Thanks again.Pederson
I am not sure if this code is what I am looking for, as it represents a partial answer to my question.Pederson
Have you seen the last link I posted ?Georas
Thank you for posting the links. The last two are pretty informative. Thank you for posting them. Working on them right now. You surely deserve a +1 from me. Thank you very much.Pederson
E
2

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()
Ecosphere answered 25/2, 2015 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.