The easiest way to solve this problem is to change your C code to use void * for this untyped memory. For example:
// Assuming this new C declaration:
int write_message_data2(
model_struct *model,
void * message_data,
size_t message_data_size
);
// here’s how to call it from Swift:
var d: Data = …
let result = d.withUnsafeMutableBytes { buf in
write_message_data2(model, buf.baseAddress!, buf.count)
}
Note that the force unwrap is safe as long as d is not empty.
If you can’t do that then you need some type conversions:
let result = d.withUnsafeMutableBytes { buf in
write_message_data(model, buf.baseAddress!.assumingMemoryBound(to: UInt8.self), Int32(buf.count))
}
Two things to note here:
assumingMemoryBound(to:) is kinda suspect in general but it’s justified here because the data isn’t an array of UInt8 but rather untyped memory.
The conversion to Int32 could trap, which is why my first example used size_t.