Search code examples
reachabilityswift3

Cannot invoke initializer for type 'UnsafeMutablePointer'


I'm trying to update Reachability.swift to swift 3.0 and I'm having trouble passing the Reachability instance to the call back function.

Here is my snippet: * please note self = Reachability class

var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)

context.info = UnsafeMutablePointer(Unmanaged.passUnretained(self).toOpaque())

Where the compiler throws an error saying:

Cannot invoke initializer for type 'UnsafeMutablePointer<_>' with an argument list of type '(UnsafeMutableRawPointer)'

Pointer conversion restricted: use '.assumingMemoryBound(to:)' or '.bindMemory(to:capacity:)' to view memory as a type.

Overloads for 'UnsafeMutablePointer<_>' exist with these partially matching parameter lists: (RawPointer), (OpaquePointer), (OpaquePointer?), (UnsafeMutablePointer), (UnsafeMutablePointer?)

What I understand I need to convert self which is of type open class Reachability: NSObject to an UnsafeMutablPointer but Im not sure how to proceed.


Solution

  • Check the type of info property from the latest reference:

    Declaration

    var info: UnsafeMutableRawPointer?
    

    And the type of toOpaque() has become UnsafeMutableRawPointer. (I couldn't have found an up-to-date Apple document, but you can check it easily in the Quick Help pane of Xcode.)

    You have no need to convert:

        context.info = Unmanaged.passUnretained(self).toOpaque()