Thomas Kilian Thomas Kilian - 20 days ago 5
Objective-C Question

Getting UnsafeBufferPointer from ObjC

In my ObjC-Swift bridging I currently use

- (double*)vector {
return (double*)self.ndArray->vector();
}

- (long) size {
return self.ndArray->size();
}


to create an array in Swift like

let p = vector.vector()
let s = vector.size()
let a = Array<Double>(UnsafeBufferPointer(start: p, count: s))


Would it be possible to get this directly from ObjC like

let a = Array<Double>(vector.readyPackedPointer())


?

Answer

A C pointer does not contain any information about the size of the pointed-to memory region, so you'll always have to pass both pointer and count from (Objective-)C to Swift. You could pass a struct containing both pointer and count to save a function call.

Comments