Thomas Kilian Thomas Kilian - 1 year ago 131
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download