How to correctly initialize an UnsafePointer in Swift? -
i'm trying use ctfontcreatepathforglyph(font: ctfont?, glyph: cgglyph, transform: cconstpointer<cgaffinetransform>)
:
let myfont = ctfontcreatewithname("helvetica", 12, nil) let myglyph = ctfontgetglyphwithname(myfont, "a") let mytransform = cgaffinetransformidentity
but how correctly pass mytransform
ctfontcreatepathforglyph
?
i've tried creating mytransformpointer
pass function so:
var mytransformpointer: unsafepointer<cgaffinetransform> = unsafepointer().initialize(newvalue: mytransform)
but error:
playground execution failed: error: <repl>:20:76: error: '()' not convertible 'unsafepointer<cgaffinetransform>' var mytransformpointer: unsafepointer<cgaffinetransform> = unsafepointer().initialize(newvalue: mytransform)
so tried explicitly naming type:
var mytransformpointer: unsafepointer<cgaffinetransform> = unsafepointer<cgaffinetransform>().initialize(newvalue: mytransform)
and different error:
playground execution failed: error: <repl>:20:95: error: not find overload 'init' accepts supplied arguments var mytransformpointer: unsafepointer<cgaffinetransform> = unsafepointer<cgaffinetransform>().initialize(newvalue: mytransform) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the auto-complete suggests should work?
the simplest solution using withunsafepointer
function:
let myfont = ctfontcreatewithname("helvetica", 12, nil) let myglyph = ctfontgetglyphwithname(myfont, "a") var mytransform = cgaffinetransformidentity var path = withunsafepointer(&mytransform) { (pointer: unsafepointer<cgaffinetransform>) -> (cgpath) in return ctfontcreatepathforglyph(myfont, myglyph, pointer) }
the initialize
not constructor. have alloc
new memory using unsafepointer<t>.alloc
, initialize
, dealloc
. function withunsafepointer
you.
note mytransform
cannot constant (var
not let
) otherwise cannot use inout
param (&mytransform
).
Comments
Post a Comment