为什么postNotification在Swift的第二次调用中失败?

时间:2016-04-13 21:59:48

标签: ios swift cllocationmanager nsnotificationcenter

我从locationManager调用一个方法来更新位置(didUpdateLocations)。 在这个方法中我使用postNotification,一切都好。 但是当我第二次调用来自locationManager的方法时,出现了一个错误,“由于未捕获的异常终止应用程序'NSInvalidArgumentException',原因:' - [ NSMallocBlock sendMessageNotification:]:无法识别的选择器发送到实例0x17026fac0' “

[错误信息变量]

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    debugLog("locations \(locations)")

    if let location = locations.last {

        let mMessage = Message()
        mMessage.latitude = location.coordinate.latitude
        mMessage.longitude = location.coordinate.longitude
        let userInfo = ["message": JSONSerializer.toJson(mMessage)]

        if !userInfo.isEmpty {
            NSNotificationCenter.defaultCenter().postNotificationName("__SEND_MESSAGE__",
                object: nil,
                userInfo: userInfo)
        }


    }

1 个答案:

答案 0 :(得分:0)

在我的代码中缺少一个删除观察者,在我添加通知的同一个类中。

deinit {
    // perform the deinitialization
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

感谢。

相关问题