ios - Swift 通知错误 : unrecognized selector sent to instance

标签 ios swift nsnotificationcenter unrecognized-selector

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

我收到这个错误:

2017-09-14 03:51:59.110379 GameTest[2671:668473] -[NSConcreteNotification length]: unrecognized selector sent to instance 0x1702503b0
2017-09-14 03:51:59.134158 GameTest[2671:668473] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteNotification length]: unrecognized selector sent to instance 0x1702503b0'
*** First throw call stack:
(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: terminating with uncaught exception of type NSException
(lldb) 

In my app delegate I have this code from

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)

enter image description here

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

最佳答案

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

我怀疑您正在(直接或间接)调用传递给选择器的对象的 length。这不是在 Notification 类型上定义的,因此是异常(exception)。

关于ios - Swift 通知错误 : unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46209809/

相关文章:

ios - 如何从导航栏滑动 UIPageViewController

ios - 在 Xamarin 中使用 transitionWithView 设置 UIView.Hidden 属性时淡入淡出动画

IOS 通知显示两次默认和我的通知警报

ios - 扩展通知中心

ios - Sprite Kit 中的顶屏 iAd

ios - 哪些 iOS 版本让您无需手动管理内存即可进行开发?

ios - 如何获取受约束拉伸(stretch)的 View 的高度? - iOS

ios - 如何使用 Swift 将通知数据传递给 View Controller

ios - 设置 UIButton 之间的距离相等

ios - 使用 NSNotificationCenter 在 VC 之间发送数据