I am having two major problems when trying to upgrade a library that involves file exporting to Swift 3. I have read the apple/swift migration page, but still unsure about what is exactly the best way to go to make sure I don't lose any of these bytes. A lot of examples are converting string, but I actually need bytes.
The first problem I am having is within every line below that starts with stream.write
, the UnsafePointer<UIntX>(&var)
returns an error saying 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);
I was able to to change this by performing changes to UnsafeRawPointer
and UnsafeBufferPointer
when creating the Data
.
After I do that I have another issue where Xcode will print the error that bytes is unavailable use withUnsafeBytes instead
.
So overall, I am able to fix minor parts piece by piece using stuff like this, but because of all the chained commands, I am having trouble making multiple changes and trying stackoverflow solutions and swift migration practices because of multiple required changes on chained commands.
I appreciate any answers or enlightment/direction.