How to pass a Swift array to an Objective-C function -
i'd pass array of cgpoint
values objective-c
function.
swift:
var mypoints:[cgpoints]? = [cgpoint(x: 0, y:0)] objcwrapper.callsomething(&mypoints)
objective-c
+ (void) callsomething: (cgpoint []) points {...}
error got:
cannot invoke 'callsomething' argument list of type 'inout [cgpoint]?)'
the problem you’ve made mypoints
optional, i.e. [cgpoint]?
instead of [cgpoint]
. code snippet, it’s not clear why you’re doing this. if there’s not need wider context, drop ?
, code should compile.
note, if objcwrapper.callsomething
function wrote , control, consider making take const cgpoint []
instead if doesn’t need change values in array. way, won’t inout
won’t need &
in front of mypoints
, , use if it’s declared let
, i.e.:
// no need give type, can inferred [cgpoint] let mypoints = [cgpoint(x: 0, y:0)] objcwrapper.callsomething(mypoints)
if declare as:
+ (void) callsomething: (const cgpoint []) points {...}
if on other hand needs optional array reasons not shown, you’ll have use unwrapping technique underlying pointer instead of using implicit interop support:
let mypoints: [cgpoint]? = [cgpoint(x: 0, y:0)] mypoints?.withunsafebufferpointer { buf->void in objcwrapper.callsomething(buf.baseaddress) }
or, if can’t change callsomething
take const array, gets quite annoying:
var mypoints: [cgpoint]? = [cgpoint(x: 0, y:0)] mypoints?.withunsafemutablebufferpointer { (inout buf: unsafemutablebufferpointer)->void in objcwrapper.callsomething(buf.baseaddress) }
Comments
Post a Comment