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<()>
is kind of void* type?
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
Post a Comment