swift - UnsafeMutablePointer to Concrete Object Type -
swift - UnsafeMutablePointer<Void> to Concrete Object Type -
how can cast unsafemutablepointer<void>
concrete object type
i've created kvo observer , passed custom class context this
class info : nsobject { } class foo : nsobject { dynamic var property = "" } var info = info() class observing : nsobject { func observe1(object: nsobject) { object.addobserver(self, forkeypath: "property", options: .new, context: &info) } override func observevalueforkeypath(keypath: string, ofobject object: anyobject, change: [nsobject : anyobject], context: unsafemutablepointer<void>) { println("observed \(keypath) change:'\(change)' context: \(context)") } } allow o = observing() var object = foo() o.observe1(object) object.property = "new value"
i know how cast context info class
unsafemutablepointer
has initializer takes unsafemutablepointer
of type, result beingness same pointer new type.
so in case:
let infoptr = unsafemutablepointer<info>(context) allow info = infoptr.memory
beware though is, docs describe it, "a fundamentally unsafe conversion". if pointer have not pointer type convert to, or if memory accessing not valid in context, may end accessing invalid memory , programme little crashy.
swift casting
Comments
Post a Comment