ios - Objective-C pointer and swift -
i'm following apple document, unfortunately examples written on objective-c, have confidence swift language , can not understand meaning of things, in particular, in example:
void runloopsourcesperformroutine (void *info){ runloopsource* obj = (runloopsource*)info; [obj sourcefired]; }
this line: runloopsource* obj = (runloopsource*)info;
the parameter: void *info
indicates info
pointer void, can put address of type of data structure, following various apple documents saw translation of : void *info
swift language :
info: unsafemutablerawpointer?
now, runloopsource* obj = (runloopsource*)info;
line indicates obj variable of type: runloopsource, , assigned value of (runloopsource *) info
, precisely mean statement? : (runloopsource *) info
, , how translates in swift language ?
what dealing (void *info
) c pointer-to-void, arrives swift form of unsaferawpointer. means type info has been cast away , memory being managed elsewhere.
in order work thing believe be, i.e. runloopsource, need characterize explicitly runloopsource. in c, cast, in example code posted: (runloopsource*)info
. in swift, rebind.
observe in case whole thing has been made little more complicated fact unsafemutablerawpointer has been wrapped in optional, , have unwrapped before can @ all.
assuming, then, in case, info
unsafemutablerawpointer?
bound runloopsource, can say:
let rlsptr = info!.assumingmemorybound(to: runloopsource.self) let rls = rlsptr.pointee
now rls
is runloopsource , can work like. keep in mind, however, memory unmanaged, should work here , now.
edit way, apple has nice document on entire matter: https://swift.org/migration-guide/se-0107-migrate.html
Comments
Post a Comment