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

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -