相关疑难解决方法(0)

如何在Swift中将Int转换为NSData?

在Objective-C中,我使用以下代码

  1. Int变量转换NSData为字节数据包.

    int myScore = 0;
    NSData *packet = [NSData dataWithBytes:&myScore length:sizeof(myScore)];
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将转换后的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并将其用作方法.我怎样才能做到这一点?

int nsdata ios swift

20
推荐指数
3
解决办法
2万
查看次数

标签 统计

int ×1

ios ×1

nsdata ×1

swift ×1