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

时间:2016-11-10 14:29:34

标签: pointers swift3 void-pointers

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

我遇到的第一个问题是在stream.write之后的每一行中,UnsafePointer<UIntX>(&var)会返回错误Ambigious use of 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);

我可以在创建UnsafeRawPointer时对UnsafeBufferPointerData进行更改,从而对此进行更改。

在我这样做后,我有另一个问题,Xcode将打印bytes is unavailable use withUnsafeBytes instead的错误。

总的来说,我能够使用这样的东西一块一块地修复小部件,但由于所有链式命令,我无法进行多项更改并尝试堆栈溢出解决方案和快速迁移实践,因为需要进行多项更改链式命令。

我感谢任何答案或启发/指导。

Other Migration Page That Was Helpful

StackOverFlow Reference 1

1 个答案:

答案 0 :(得分:0)

通常,您无需创建中间Column_name Type Computed Nullable -------------------------------------------- id int no no is_active bit no yes calc_flag1 bit yes no calc_falg2 bit yes yes calc_falg_int int yes no 来将字节序列写入Data

OutputStream