嗨,我遇到了一个问题,当我将连接从wifi更改为蜂窝移动时,反之亦然,我的应用程序崩溃了,而不是像我已经编程过的那样显示“No Connection”视图控制器。我收到这样的错误:

由于未捕获的异常'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()


    }
}

谁能告诉我如何解决此问题?
谢谢

最佳答案

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

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

07-26 09:40