Pointers Swift 3不安全指针不明确初始化和字节

Pointers Swift 3不安全指针不明确初始化和字节,pointers,swift3,void-pointers,Pointers,Swift3,Void Pointers,当我试图升级一个涉及文件导出到Swift 3的库时,我遇到了两个主要问题。我已经阅读了,但仍然不确定什么才是确保不会丢失这些字节的最佳方法。很多例子都在转换字符串,但实际上我需要字节 我遇到的第一个问题是在下面以stream.write开头的每一行中,UnsafePointer(&var)返回一个错误,表示init的使用含糊不清 var xbuffer = [UInt8](repeating: 0, count: 2048 + 171*1024) let stream : OutputStrea

当我试图升级一个涉及文件导出到Swift 3的库时,我遇到了两个主要问题。我已经阅读了,但仍然不确定什么才是确保不会丢失这些字节的最佳方法。很多例子都在转换字符串,但实际上我需要字节

我遇到的第一个问题是在下面以
stream.write开头的每一行中,
UnsafePointer(&var)
返回一个错误,表示init的使用含糊不清

var xbuffer = [UInt8](repeating: 0, count: 2048 + 171*1024)
let stream : OutputStream = OutputStream(toBuffer: &xbuffer, capacity: 2048 + 171*1024);
stream.open();

//Version number (2 bytes)
stream.write(Data(bytes: UnsafePointer<UInt8>(&(self.version)), count: 1).bytes.bindMemory(to: UInt8.self, capacity: Data(bytes: UnsafePointer<UInt8>(&(self.version)), count: 1).count),maxLength: 1);

stream.write(Data(bytes: UnsafePointer<UInt8>(&(self.subVersion)), count: 1).bytes.bindMemory(to: UInt8.self, capacity: Data(bytes: UnsafePointer<UInt8>(&(self.subVersion)), count: 1).count),maxLength: 1);

//Operation ID status code (2 bytes)
var opId_big = CFSwapInt16HostToBig(self.operationId);
stream.write(Data(bytes: UnsafePointer<UInt8>(&(opId_big)), count: 2).bytes.bindMemory(to: UInt8.self, capacity: Data(bytes: UnsafePointer<UInt8>(&(opId_big)), count: 2).count),maxLength: 2);

//Request ID (4 bytes)
requestId = requestId | 0x1;
var rqid_big = CFSwapInt32HostToBig(requestId);
stream.write(Data(bytes: UnsafePointer<UInt8>(&(rqid_big)), count: 4).bytes.bindMemory(to: UInt8.self, capacity: Data(bytes: UnsafePointer<UInt8>(&(rqid_big)), count: 4).count), maxLength: 4);
var xbuffer=[UInt8](重复:0,计数:2048+171*1024)
let stream:OutputStream=OutputStream(toBuffer:&xbuffer,容量:2048+171*1024);
stream.open();
//版本号(2字节)
stream.write(数据(字节:UnsafePointer(&(self.version)),计数:1.bytes.bindMemory(to:UInt8.self,容量:数据(字节:UnsafePointer(&(self.version)),计数:1.count),maxLength:1);
stream.write(数据(字节:UnsafePointer(&(self.subVersion)),计数:1.bytes.bindMemory(to:UInt8.self,容量:数据(字节:UnsafePointer(&(self.subVersion)),计数:1.count),maxLength:1);
//操作ID状态代码(2字节)
var opId_big=CFSwapInt16HostToBig(自操作ID);
stream.write(数据(字节:UnsafePointer(&(opId_big)),计数:2)、bytes.bindMemory(to:UInt8.self,容量:数据(字节:UnsafePointer(&(opId_big)),计数:2)、maxLength:2);
//请求ID(4字节)
requestId=requestId | 0x1;
var rqid_big=CFSwapInt32HostToBig(请求ID);
stream.write(数据(字节:UnsafePointer(&(rqid_big)),计数:4)、bytes.bindMemory(to:UInt8.self,容量:数据(字节:UnsafePointer(&(rqid_big)),计数:4)、maxLength:4);
在创建
数据时,我可以通过对
UnsafeRawPointer
UnsafeBufferPointer
执行更改来改变这一点

在我这样做之后,我遇到了另一个问题,Xcode会打印出一个错误,即
字节不可用,请改为使用unsafebytes

因此,总体而言,我能够使用类似这样的东西逐个修复小部件,但由于所有链接命令,我在进行多次更改和尝试stackoverflow解决方案和快速迁移实践时遇到困难,因为链接命令上需要多次更改

我很感激任何答案或启示/指导


通常,您无需创建中间
数据
,即可将字节序列写入
输出流

    var xbuffer = [UInt8](repeating: 0, count: 2048 + 171*1024)
    xbuffer.withUnsafeMutableBufferPointer{bufferPointer in
        let stream : OutputStream = OutputStream(toBuffer: bufferPointer.baseAddress!, capacity: xbuffer.count);
        stream.open();

        //Version number (2 bytes)
        withUnsafeBytes(of: &self.version) {
            stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 1)
        }

        withUnsafeBytes(of: &self.subVersion) {
            stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 1)
        }

        //Operation ID status code (2 bytes)
        var opId_big = self.operationId.bigEndian
        withUnsafeBytes(of: &opId_big) {
            stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 2)
        }

        //Request ID (4 bytes)
        var rqid_big = (requestId | 0x1).bigEndian;
        withUnsafeBytes(of: &rqid_big) {
            stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 4)
        }
    }

通常,您无需创建中间
数据
,即可将字节序列写入
输出流

    var xbuffer = [UInt8](repeating: 0, count: 2048 + 171*1024)
    xbuffer.withUnsafeMutableBufferPointer{bufferPointer in
        let stream : OutputStream = OutputStream(toBuffer: bufferPointer.baseAddress!, capacity: xbuffer.count);
        stream.open();

        //Version number (2 bytes)
        withUnsafeBytes(of: &self.version) {
            stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 1)
        }

        withUnsafeBytes(of: &self.subVersion) {
            stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 1)
        }

        //Operation ID status code (2 bytes)
        var opId_big = self.operationId.bigEndian
        withUnsafeBytes(of: &opId_big) {
            stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 2)
        }

        //Request ID (4 bytes)
        var rqid_big = (requestId | 0x1).bigEndian;
        withUnsafeBytes(of: &rqid_big) {
            stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 4)
        }
    }

今晚我要试试这个。谢谢你的回答。我是swift的新手,很久以前就用objective-c编写了这个库的核心。这将是超级有用的,如果它的工作!今晚我要试试这个。谢谢你的回答。我是swift的新手,很久以前就用objective-c编写了这个库的核心。这将是超级有用的,如果它的工作!