Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么在Swift中第二次调用后通知失败?_Ios_Swift_Cllocationmanager_Nsnotificationcenter - Fatal编程技术网

Ios 为什么在Swift中第二次调用后通知失败?

Ios 为什么在Swift中第二次调用后通知失败?,ios,swift,cllocationmanager,nsnotificationcenter,Ios,Swift,Cllocationmanager,Nsnotificationcenter,首先,我从locationManager调用一个方法来更新位置(didUpdateLocations)。 在这个方法中,我使用postNotification,一切正常。 但是,当我第二次从locationManager调用该方法时,出现了一个错误,“由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'-[NSMallocBlocksendMessageNotification:::]:未识别的选择器发送到实例0x17026fac0'” [信息变量错误]

首先,我从locationManager调用一个方法来更新位置(didUpdateLocations)。 在这个方法中,我使用postNotification,一切正常。 但是,当我第二次从locationManager调用该方法时,出现了一个错误,“由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'-[NSMallocBlocksendMessageNotification:::]:未识别的选择器发送到实例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)
        }


    }

在我的代码中,在我添加通知的同一个类中缺少了一个remove observer

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

谢谢。

请显示您的代码和堆栈跟踪,但看起来您是在以某种方式引用已释放的内存。我添加了该方法,但在堆栈跟踪中,由于未捕获的异常“NSInvalidArgumentException”,只说终止应用程序,原因:'-[NSMallocBlock sendMessageNotification:]:无法识别的选择器发送到实例0x17026fac0'为什么要将对象序列化为JSON?您可以将消息对象添加到字典中。事实上,您可以只添加location或location.coordinate object,事情是我将Json数据发送到服务器,然后您应该在发送到服务器之前将其转换为Json。在这里这样做没有意义。不管怎样,都不应该有任何理由认为此代码会导致崩溃。您需要查看堆栈轨迹,以查看它在代码中试图调用该函数的位置
sendMessageNotification