1

我目前正在使用 xCode8 beta6(swift3) 中的可达性库开发一个项目。我想我在我的项目中正确地实现了 Reachability.swift。

顺便说一句,当我调用时,应用程序在 Reachability.swift 的以下行中崩溃reachability.startNotifier()

let reachability = Reachability()!
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:",name: ReachabilityChangedNotification,object: reachability)
    do{
      try reachability.startNotifier()
    }catch{
      print("could not start reachability notifier")
    }

在此处输入图像描述 这是我在日志中可以看到的内容。

*** NSForwarding:警告:类 'WebClient' 的对象 0x10d939668 未实现 methodSignatureForSelector:- 前面的麻烦无法识别的选择器 +[WebClient 可达性更改:]

当然,我确实实现了reachabilityChanged 选择器功能。

func reachabilityChanged(note: NSNotification) {

        let reachability = note.object as! Reachability

        if reachability.isReachable {
            if self.pendingSurvey == true {
               ....
            }
        }
    }

我花了很多时间寻找原因,但我无法弄清楚。

4

1 回答 1

0

在 Swift 3 中,Objective-C 选择器func reachabilityChanged(note: NSNotification)变成了reachabilityChangedWithNote:. (这在某些测试版中可能会有所不同。)因此,Reachability运行时找不到选择器的方法reachabilityChange:并崩溃。

reachabilityChange:通常你在 Swift 3 中为选择器声明一个 Swift 方法为:

func reachabilityChanged(_ note: NSNotification) {
    //
}

或者使用@objc是另一种方式:

@objc(reachabilityChange:)
func reachabilityChanged(note: NSNotification) {
    //
}
于 2016-08-24T21:22:57.580 回答