不能在没有参数的情况下为“可达性”类型调用初始值设定项

时间:2023-01-23 22:05:35

I want to use the ReachabilitySwift class in my application. I added the dependency in my podFile and I get the code from github.

我想在我的应用程序中使用ReachabilitySwift类。我在podFile中添加了依赖项,然后从github获取代码。

I got this error:

我收到了这个错误:

cannot invoke initializer for type 'Reachability' with no argument" on this line

无法在此行上调用“Reachability”类型的初始化程序,且不带参数

My code:

我的代码:

let reachability = Reachability()!

让可达性=可达性()!

Can anyone help me please?

有人可以帮我吗?

2 个解决方案

#1


1  

let reachability = Reachability(hostname: "http://whatYouWantToCheck.com")

or try a pod update

或尝试pod更新

example from github

来自github的例子

let reachability = Reachability()!

reachability.whenReachable = { reachability in
    // this is called on a background thread, but UI updates must
    // be on the main thread, like this:
    dispatch_async(dispatch_get_main_queue()) {
        if reachability.isReachableViaWiFi() {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    }
}

#2


1  

Thanks to the answer in this link and also this one, my project is running perfectly and my problem is solved.

感谢这个链接和这个链接的答案,我的项目运行完美,我的问题解决了。

By doing this: var reachability: Reachability! I can check the network reachability without using any host for the check.

通过这样做:var reachability:可达性!我可以在不使用任何主机进行检查的情况下检查网络可达性。

#1


1  

let reachability = Reachability(hostname: "http://whatYouWantToCheck.com")

or try a pod update

或尝试pod更新

example from github

来自github的例子

let reachability = Reachability()!

reachability.whenReachable = { reachability in
    // this is called on a background thread, but UI updates must
    // be on the main thread, like this:
    dispatch_async(dispatch_get_main_queue()) {
        if reachability.isReachableViaWiFi() {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    }
}

#2


1  

Thanks to the answer in this link and also this one, my project is running perfectly and my problem is solved.

感谢这个链接和这个链接的答案,我的项目运行完美,我的问题解决了。

By doing this: var reachability: Reachability! I can check the network reachability without using any host for the check.

通过这样做:var reachability:可达性!我可以在不使用任何主机进行检查的情况下检查网络可达性。