unexpected non void return value in void function

Pippo

I've been pouring over stack overflow for ages trying to find a way out of this error:

unexpected non void return value in void function

that I am getting with returning a Bool within my function.

i just can't seem to dig my way out of this one. I'm sure its something to do with async but I'm not very familiar with these types of functions.

class CheckReachability {


    class func setupReachability (hostName:String?, useClosures: Bool) -> Bool{

        var reachability : Reachability!
        var connected = false

                let reachability2 = hostName == nil ? Reachability() : Reachability(hostname: hostName!)
                reachability = reachability2
                try! reachability?.startNotifier()

                if useClosures {
                    reachability2?.whenReachable = { reachability in
                        DispatchQueue.main.async {
                            connected = true
                            print("Reachable....")
                        }
                    }
                    reachability2?.whenUnreachable = { reachability in
                        DispatchQueue.main.async {
                            connected = false
                            print("Not Connected....")
                        }
                    }

                } else {
                    NotificationCenter.default.addObserver(self, selector: Selector(("reachabilityChanged:")), name: ReachabilityChangedNotification, object: reachability2)
                }
            return connected
            }

}

calling this from viewdidload on another vc doesn't allow enough time to get a true result

        let connected = CheckReachability.setupReachability(hostName: nil, useClosures: true)
if connected {
bubuxu

If you want to write a checking class to listen to the reachability, define it as a singleton and pass the completeBlock to it like this:

class CheckReachability {

    static let shared = CheckReachability()

    var reachability: Reachability?

    func setupReachability(hostName:String?, completeBlock: ((Bool) -> Void)? = nil) {

        reachability = hostName == nil ? Reachability() : Reachability(hostname: hostName!)

        try? reachability?.startNotifier()

        if let block = completeBlock {
            reachability?.whenReachable = { reachability in
                DispatchQueue.main.async {
                    print("Reachable....")
                    block(true)
                }
            }
            reachability?.whenUnreachable = { reachability in
                DispatchQueue.main.async {
                    print("Not Connected....")
                    block(false)
                }
            }
        } else {
            // If we don't use block, there is no point to observe it.
            NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(_:)), name: .ReachabilityChangedNotification, object: nil)
        }

    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    @objc func reachabilityChanged(_ notification: Notification) {
        // ?? what should we do here?
    }

} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Calling a non-void function without using its return value. What actually happens?

unexpected non-void return value in void function in new Swift class

Unexpected non-void return value in void function

Unexpected Non-Void Return Value In Void Function (Swift 2.0)

Type No return, in function returning non-void

Why does Unexpected non-void return value in void function happen?

Unexpected non-void return value in void function Swift3

Return value void * in a function

Warning 'return' with no value, in function returning non-void - What should it return?

swift Non-void function should return a value in a guard let

Unexpected non-void return value in void function (Swift 3)

Restrict function return value to void

Non-void function potential lack of return

Retrieve product localised price - Unexpected non-void return value in void function

What's the return value of a non-void function missing return statement?

Unexpected non-void return value in void function - Swift 4 (Firebase, Firestore)

Should i return a value for a Future<void> Function?

Is the return command in a non-void function necessary?

What is the return value if we dont return anything from a non void return typed function in c++?[Experimental]

void function pointer return value in C

Unclear about return value of a void function in C

Why does a void function return a value?

why in this code would I be getting "Unexpected non-void return value in void function"

Class understanding: Unexpected non-void return value in void function (swift 3)

Unexpected non-void return value in void function(swift3)

Unexpected non-void return value in void function swift 4 using json serialisation

Swift NumberOfRowsInSection Unexpected non-void return value in void function

How to return a value from Void Function?

'Unexpected non-void return value in void function' in swiftui