在Objective-C中,我使用以下代码
将Int变量转换NSData为字节数据包.
int myScore = 0;
NSData *packet = [NSData dataWithBytes:&myScore length:sizeof(myScore)];
Run Code Online (Sandbox Code Playgroud)将转换后的NSData变量用于方法.
[match sendDataToAllPlayers:
packet withDataMode: GKMatchSendDataUnreliable
error: &error];
Run Code Online (Sandbox Code Playgroud)我尝试将Objective-C代码转换为Swift:
var myScore : Int = 0
func sendDataToAllPlayers(packet: Int!,
withDataMode mode: GKMatchSendDataMode,
error: NSErrorPointer) -> Bool {
return true
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法将Int变量转换为a NSData并将其用作方法.我怎样才能做到这一点?