3

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.

the Reverend
  • 12,305
  • 10
  • 66
  • 121
  • 1
    at the callback `func callback(reachability: SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) { print("--- callback") guard let info = info else { return } let reachability = Unmanaged.fromOpaque(info).takeUnretainedValue() DispatchQueue.main.async { reachability.reachabilityChanged() } }` – Leo Dabus Aug 31 '16 at 23:02
  • 1
    https://www.dropbox.com/s/d6uc8z8kij1zmmf/Reachability%20Swift%203.zip?dl=1 – Leo Dabus Aug 31 '16 at 23:04
  • 1
    @LeoDabus thank you thats awesome! – the Reverend Aug 31 '16 at 23:14
  • Compare also http://stackoverflow.com/a/30788165/1187415, which was updated for Swift 3 some time ago. – Martin R Sep 20 '16 at 17:58

1 Answers1

3

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()
OOPer
  • 47,149
  • 6
  • 107
  • 142