ios - Casting between different UnsafePointer<T> in swift -


i trying use c-api swift. example c-api goes ways:

void dothingsonrawdata(const unsigned char* data); 

swift converts to:

void dothingsonrawdata(unsafepointer<uint8>); 

now want pass data nsdata function. nsdata.byte returns type:

unsafepointer<()> 
  1. is kind of void* type?

  2. at least swift won't accept unsafepointer<uint8>. do cast this?

struct unsafepointer<t> has constructor

/// convert unsafepointer of different type. /// /// fundamentally unsafe conversion. init<u>(_ from: unsafepointer<u>) 

which can use here

dothingsonrawdata(unsafepointer<uint8>(data.bytes)) 

you can omit generic type because inferred context:

dothingsonrawdata(unsafepointer(data.bytes)) 

update swift 3: of xcode 8 beta 6, cannot convert directly between different unsafe pointers anymore.

for data: nsdata, data.bytes unsaferawpointer can converted unsafepointer<uint8> assumingmemorybound:

dothingsonrawdata(data.bytes.assumingmemorybound(to: uint8.self)) 

for data: data simpler:

data.withunsafebytes { dothingsonrawdata($0) } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -