1

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.

Other Migration Page That Was Helpful

StackOverFlow Reference 1

Community
  • 1
  • 1
napkinsterror
  • 1,915
  • 4
  • 18
  • 27

1 Answers1

0

Generally, you have no need to create intermediate Data to write byte sequences to OutputStream.

    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)
        }
    }
OOPer
  • 47,149
  • 6
  • 107
  • 142
  • I will try this tonight. Thanks for the answer. I am new to swift and wrote the core of this library in objective-c a long time ago. This would be super helpful if it works! – napkinsterror Nov 13 '16 at 04:15