2

As Swift 2.3 to Swift 3.0 conversion raise many issue, I am trying to solve this issue but not getting solution so far.

Cannot convert value of type '(SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutablePointer) -> ()' to expected argument type 'SCNetworkReachabilityCallBack?'

Here is my code :

func callback(_ reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutablePointer<Void>) {
    let reachability = Unmanaged<Reachability>.fromOpaque(info).takeUnretainedValue()

    DispatchQueue.main.async {
        reachability.reachabilityChanged(flags)
    }
}

In startNotifier function we are passing callback, but it generates error.

public func startNotifier() throws {

    guard !notifierRunning else { return }

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

    //THIS LINE GENERATES ERROR WARNING
    if !SCNetworkReachabilitySetCallback(reachabilityRef!, callback, &context) {
        stopNotifier()
        throw ReachabilityError.unableToSetCallback
    }

    if !SCNetworkReachabilitySetDispatchQueue(reachabilityRef!, reachabilitySerialQueue) {
        stopNotifier()
        throw ReachabilityError.unableToSetDispatchQueue
    }

    // Perform an intial check
    reachabilitySerialQueue.async { () -> Void in
        let flags = self.reachabilityFlags
        self.reachabilityChanged(flags)
    }

    notifierRunning = true
}

This code generates error in above function.

    //THIS LINE GENERATES ERROR WARNING
    if !SCNetworkReachabilitySetCallback(reachabilityRef!, callback, &context) {
        stopNotifier()
        throw ReachabilityError.unableToSetCallback
    }

enter image description here

I also go through this in depth post of Martin, but not getting solution. Any help should be appreciable. Thanks in Advance.

Community
  • 1
  • 1
technerd
  • 14,144
  • 10
  • 61
  • 92
  • The referenced Q&A has code which compiles in the latest Xcode 8 beta 6, and even code for previous beta releases in the edit history. – Martin R Sep 06 '16 at 13:11

1 Answers1

2

If you have something odd in Swift 3, always check the latest reference: (As for now, the latest reference is up to the latest Xcode 8, beta 6. If you are using beta 5 or older, the code below does not work.)

Declaration

typealias SCNetworkReachabilityCallBack = (
    SCNetworkReachability,
    SCNetworkReachabilityFlags,
    UnsafeMutableRawPointer?) -> Void

The type of the last parameter of the call back has changed to UnsafeMutableRawPointer?.

So, you may need to change your callback to something like this:

func callback(_ reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) {
    let reachability = Unmanaged<Reachability>.fromOpaque(info!).takeUnretainedValue()

    DispatchQueue.main.async {
        reachability.reachabilityChanged(flags)
    }
}
OOPer
  • 47,149
  • 6
  • 107
  • 142
  • 1
    This is not working, In Swift 3.0, declaration looks like this : `public typealias SCNetworkReachabilityCallBack = @convention(c) (SCNetworkReachability, SCNetworkReachabilityFlags, UnsafeMutablePointer?) -> Swift.Void`. So it complains `Use of undeclared type UnsafeMutableRawPointer`. – technerd Sep 06 '16 at 12:36
  • @technerd, aren't you using an older beta before beta 5? – OOPer Sep 06 '16 at 12:45
  • I am using Xcode 8 , beta 4 – technerd Sep 06 '16 at 12:50
  • @technerd: The "unsafe pointer" APIs changed several times in Swift 3. My answer http://stackoverflow.com/a/30788165/1187415 that you are referring to is updated for the latest Xcode 8 beta 6. If it does not work for you then it would be nice to leave a comment at my answer to that I can fix it. – Martin R Sep 06 '16 at 12:53
  • @technerd, beta 6 was a big change for Swift. And I think GM version of Swift is very near to beta 6 than beta 4. I will add some note for who are using early betas, but I strongly recommend you to move to the latest beta and get ready for GM release. – OOPer Sep 06 '16 at 12:55
  • @OOPer : So this answer will work with latest Xcode beta ? – technerd Sep 06 '16 at 12:57
  • @technerd, tested in Xcode 8 beta 6, I'm so sure. – OOPer Sep 06 '16 at 12:59
  • @technerd: ... as does http://stackoverflow.com/a/30788165/1187415, which you already referred to. And versions for previous beta releases of Xcode 8 are in the edit history of that answer. – Martin R Sep 06 '16 at 12:59
  • @MartinR :I will check with latest Xcode beta, If there is any issue, i will update on your answer. Thanks to both of you. – technerd Sep 06 '16 at 12:59