Inna Black
Inna Black

Reputation: 251

UnsafeMutablePointer<Bytef> swift3

The follow line in swift3 make error.

out = UnsafeMutablePointer<Bytef>(data.mutableBytes)

Cannot invoke initializer for type 'UnsafeMutablePointer<Bytef>' with an argument list of type '(UnsafeMutableRawPointer)'

Upvotes: 0

Views: 1277

Answers (1)

Jeef
Jeef

Reputation: 27265

I'm not 100% sure I understand what you are asking, however, the way to handle data pointers has changed somewhat in swift3:

Swift2

When I wanted to access the actual bytes out of a data stream in swift2 you would do some funky pointer stuff casting the data into an unsafe mutable pointer, and then pointing to an array of [UInt8] which referenced all the bytes in the data stream directly.

From what you are asking it appears you may have been trying to do something similar in the past.

Swift3

You now have two commands withUnsafeBytes and withUnsafeMutableBytes. In swift2 I used to work with data by taking the bytes and casting it into a pointer array (similar to what it looks like you were doing).

In swift 3 you can now do stuff like this:

return data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> Bool in
        return 1 ==  Int((bytes[1] & 0x2) >> 1)
}

Or this:

rawData.withUnsafeMutableBytes {
            (bytes: UnsafeMutablePointer<UInt8>) -> Void in
            bytes[0] = newValue.rawValue
        }

Is this what you are looking for?

Upvotes: 1

Related Questions