John Astion John Astion - 1 year ago 144
Swift Question

Using bytes in swift3

float *vertexBuffer = (float *);

'bytes' is unavailable: use withUnsafeBytes instead

but i don't know how to use it

_ = positionSource?.data.withUnsafeBytes({ (<#UnsafePointer<ContentType>#>) -> ResultType in


Answer Source

withUnsafeBytes is a generic method, the ContentType is inferred from the type of the closure. With

data.withUnsafeBytes { (vertexBuffer: UnsafePointer<Float>) in
    // Use  vertexBuffer ...

you'll get a UnsafePointer<Float> pointing to the bytes in the data. Note that this pointer must not be used outside of the closure.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download