在Swift中从Wifi切换到Cellular时的可达性错误

时间:2015-09-09 04:52:47

标签: ios objective-c swift reachability unrecognized-selector

嗨我有一个问题,当我将我的连接从wifi更改为手机或反之亦然时,我的应用程序崩溃,而不是显示“无连接”视图控制器,就像我已编程它。我收到这样的错误:

  

由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [Spotter.AppDelegate checkReachability]:无法识别的选择器发送到实例0x17404b1f0'

我正在使用Apple提供的Objective-C示例,顺便将它与我的Swift代码连接起来。

这是我的代码:

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate {

var window: UIWindow?
var reachability : Reachability?



    NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkReachability", name: kReachabilityChangedNotification, object: nil);

    self.reachability = Reachability.reachabilityForInternetConnection();

    self.reachability!.startNotifier();

return true

}

func checkReachability(notification:NSNotification)
{

    var remoteHostStatus = self.reachability!.currentReachabilityStatus()

    if (remoteHostStatus.value == NotReachable.value)

    {

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let offlineview = mainStoryboard.instantiateViewControllerWithIdentifier("OfflineViewController") as! OfflineViewController
        window!.rootViewController = offlineview
        window!.makeKeyAndVisible()



    }else{

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("TabBar") as! UITabBarController

        window!.rootViewController = initialViewController
        window!.makeKeyAndVisible()


    }
}

有谁能告诉我如何解决这个问题? 感谢

1 个答案:

答案 0 :(得分:2)

您的选择器缺少名称中的冒号。它应该是:

NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkReachability:", name: kReachabilityChangedNotification, object: nil);