Swift 2 - UnsafeMutablePointer<Void> to object

Ashley Mills

If I have a method like:

func someMethod(contextPtr: UnsafeMutablePointer<Void>)

how do I get the object from the contextPtr?

func someMethod(contextPtr: UnsafeMutablePointer<Void>){    
    let object:MyObject = contextPtr.memory
}

gives:

'Void' is not convertible to 'MyObject'

What's the secret sauce


More detail:

What I'm actually doing here is setting up a global callback function for SCNetworkReachability:

func callback(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutablePointer<Void>) {

    let r:Reachability = info.memory
}

and then adding the callback as follows:

var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
var s = self
withUnsafeMutablePointer(&s) {
    context.info = UnsafeMutablePointer($0)
}
SCNetworkReachabilitySetCallback(reachability, callback, &context)
Martin R

This should work: pass the object pointer as an opaque unmanaged pointer to the callback:

context.info = UnsafeMutablePointer(Unmanaged.passUnretained(myObject).toOpaque())
SCNetworkReachabilitySetCallback(reachability, callback, &context) 

and retrieve in the callback via:

func callback(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutablePointer<Void>) {

    let myObject = Unmanaged<MyObject>.fromOpaque(COpaquePointer(info)).takeUnretainedValue()

}

Of course this assumes that some strong reference to the object exists as long as the callback is installed, so that the object is not deallocated.

Update: Note that both conversions from object pointer to void pointer and back can be simplified if you are willing to use "unsafe" functions:

context.info = unsafeAddressOf(myObject)
// ...
myObject = unsafeBitCast(info, MyObject.self)

The generated assembly code is – as far as I can see – identical.

Update 2: See also How to cast self to UnsafeMutablePointer<Void> type in swift for more information about the "bridging" and some helper functions which can be used here.


Swift 3 update (Xcode 8 beta 6):

var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
context.info = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())

// ...

func callback(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) {
    if let info = info {
        let myObject = Unmanaged<MyObject>.fromOpaque(info).takeUnretainedValue()
        // ...
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cast a Swift struct to UnsafeMutablePointer<Void>

UnsafeMutablePointer<Void> to Concrete Object Type

How to cast self to UnsafeMutablePointer<Void> type in swift

Using UnsafeMutablePointer and CFRunLoopObserverContext in swift 2

How to create UnsafeMutablePointer<CGPoint> Object in swift

Swift get value from UnsafeMutablePointer<Void> using UnsafePointer<String>

copy NSData to UnsafeMutablePointer<Void>

How to create UnsafeMutablePointer<UnsafeMutablePointer> in swift

Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>

Typecast UnsafeMutablePointer<Void> to UnsafeMutablePointer<#Struct type#>

How to get length of a UnsafeMutablePointer<Object> written to a OpenSSL BIO in Swift?

UnsafeMutablePointer<CFTypeRef> in Swift 3

UnsafeMutablePointer to expected argument type UnsafeMutablePointer<_> in Swift 3

What is UnsafeMutablePointer<Void>? How to modify the underlying memory?

Cast UnsafeMutablePointer<Void> to my struct type

Error in UnsafeMutablePointer in swift3

How to use UnsafeMutablePointer in Swift 3?

What is UnsafeMutablePointer<Unmanaged<CMSampleBuffer>?> in Swift?

How to use UnsafeMutablePointer<OpaquePointer> in Swift?

UnsafeMutablePointer<Bytef> swift3

How to represent Objc-C NSUInteger[] = {0,1,2} in Swift as UnsafeMutablePointer<UInt>?

Swift: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'

How to return nil object in non void function - swift 2.1?

In Swift 3.1, UnsafeMutablePointer.initialize(from:) is deprecated

Swift convert Data to UnsafeMutablePointer<Int8>

Cast a swift dictionary ( [String:AnyObject] ) to a UnsafeMutablePointer

Convert UnsafeMutableRawPointer to UnsafeMutablePointer<T> in swift 3

Swift convert string to UnsafeMutablePointer<Int8>

Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift