【问题标题】:Swift Notification error: unrecognized selector sent to instanceSwift 通知错误:无法识别的选择器发送到实例
【发布时间】:2017-09-14 02:59:06
【问题描述】:

出于某种原因,这才刚刚开始发生。我今天唯一做的就是添加启动图像。

我收到此错误:

2017-09-14 03:51:59.110379 GameTest[2671:668473]-[NSConcreteNotification 长度]:无法识别的选择器发送到实例 0x1702503b0 2017-09-14 03:51:59.134158 GameTest[2671:668473] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSConcreteNotification 长度]:无法识别的选择器发送到实例 0x1702503b0” *** 首先抛出调用堆栈: (0x1895581c0 0x187f9055c 0x18955f278 0x18955c278 0x18945659c 0x1010327ac 0x100ff8738 0x10145dcf8 0x1000711d0 0x1894f222c 0x1894f1930 0x1894f16ac 0x189560b9c 0x189433bf4 0x189f396bc 0x1000756f8 0x10004bc3c 0x10175525c 0x10175521c 0x10175a284 0x189505f2c 0x189503b18 0x189432048 0x18aeb3198 0x18f395bd0 0x18f390908 0x100078d68 0x1884145b8) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)

在我的应用委托中,我有这段代码来自

reachability.whenReachable = { reachability in
    // this is called on a background thread, but UI updates must
    // be on the main thread, like this:
    DispatchQueue.main.async {
        if reachability.isReachableViaWiFi {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "hasInternet"), object: nil)
        } else {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "hasInternet"), object: nil)
        }
    }
}
reachability.whenUnreachable = { reachability in
    // this is called on a background thread, but UI updates must
    // be on the main thread, like this:
    DispatchQueue.main.async {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "noInternet"), object: nil)
    }
}

错误似乎是这一行:

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "noInternet"), object: nil)

有人可以帮我解决这个问题吗?其他问题似乎与通知无关,我发现它们没有用。

【问题讨论】:

  • 你能贴出观察noInternet通知的东西的代码吗?我的倾向是您传递给该方法的选择器可能不正确。
  • 我遇到了同样的问题(打电话时崩溃)。对我来说似乎很疯狂,因为这太基础了,而且我之前使用过 NotificationCenter 很多次。

标签: ios swift nsnotificationcenter unrecognized-selector


【解决方案1】:

检查您传递给NotificationCenter.addObserver(_:selector:name:object) 的选择器的参数类型。选择器必须有一个且只有一个参数,Notification 的实例。

我怀疑您正在(直接或间接地)在传递给选择器的对象上调用lengthNotification 类型上没有定义,因此例外。

【讨论】:

    【解决方案2】:

    检查您收听通知的选择器名称,可能在末尾缺少“:”以接收通知参数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多