In Swift, a uuid_t
is defined as a tuple of 16 UInt8 elements. How do I get an UnsafePointer to that tuple to pass to an Objective-C class?
The Objective-C class is defined as follows:
@interface Writer
- (void)writeUUID:(_Nonnull uuid_t)value;
@end
The auto-generated Swift 5 interface is defined as follows:
open class Writer : NSObject {
open func writeUUID(_ value: UnsafeMutablePointer<UInt8>)
}
Example of trying to create a UUID
and then passing its uuid_t
to an Objective-C class:
let uuid = UUID().uuid
// Error: Cannot convert value of type 'UnsafeMutablePointer<_>'
// to expected argument type 'UnsafeMutablePointer<UInt8>'
withUnsafeMutablePointer(to:u) {
encoder.writeUUID($0)
}
// Error: Cannot convert value of type '(UnsafeMutablePointer<UInt8>) -> Void'
// to expected argument type '(UnsafeMutablePointer<_>) -> _'
withUnsafeMutablePointer(to:u) { (ptr:UnsafeMutablePointer<UInt8>) in
encoder.writeUUID(ptr)
}
How can I pass a uuid_t from Swift to Objective-C and back?