void函数中的意外非void返回值

时间:2017-01-01 11:54:38

标签: ios swift xcode asynchronous closures

我一直在倾倒堆栈溢出多年,试图找到摆脱这个错误的方法:

  

void函数中的意外非void返回值

我在我的函数中返回Bool

我似乎无法从这一个中挖掘出来。我确定它与异步有关,但我对这些类型的函数并不是很熟悉。

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
            }

}

在另一个vc上从viewdidload调用它不会有足够的时间来获得真实的结果

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

2 个答案:

答案 0 :(得分:2)

您的问题很混乱,因为您发布的代码没有您描述的错误。但是,您正在尝试创建一个从异步函数返回结果的函数。这不是异步的工作方式。

异步函数开始在后台执行任务,该任务在返回之前不会完成。

你需要调整自己的想法。您需要编写函数来获取完成处理程序,而不是尝试从函数返回结果。然后在长时间运行的任务完成后调用完成处理程序(在函数返回之后)。

@bubuxu为您提供了显示如何修改您的功能的代码。

答案 1 :(得分:1)

如果您想编写一个检查类来监听可达性,请将其定义为单例并将completeBlock传递给它,如下所示:

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?
    }

}