【问题标题】:How to access userInfo property in from a Notification?如何从通知中访问 userInfo 属性?
【发布时间】:2017-07-19 18:59:14
【问题描述】:

我调用NotificationCenter的post方法时有个方法:

func processMessage(message: CocoaMQTTMessage) {
    Log.d("DestinationTopicHandler: handling message")
    //message.string: Contains the response of the server
    print(message.string!) // print the response (String)

    bus.post(name: .DestinationRespReceived, userInfo: [message.string! : String()])
}

这里是我的 message.string 打印:

{ “req_token”:“测试”, “conv_token”:“测试”, "ts":"2017-04-05 12:00:00.123", “代码”:0, "message":"ACCESO CONCEDIDO", “auth_token”:“测试”, “回复”:{ “类型”:“测试”, “数据”:{ “命运”:[ { "idDestino":"1", “desDestino”:“亚松森” }, { "idDestino":"2", “desDestino”:“迈阿密” } ] } } }

这是我的 post 方法声明:

func post(name: Notification.Name, userInfo info : [AnyHashable : Any]? = nil) {
    NotificationCenter.default
        .post(name: name, object: nil, userInfo: info)
}

到目前为止一切正常。这是我尝试访问 userInfo 数据的最后一种方法:

public func responseReceived(_ notification: Notification) {
    if processed {
        return
    }

    processed = true
    responseReceived = true
    Log.i("responseReceived:")

    guard let userInfo = notification.userInfo, let msg = userInfo["idDestino"] as? String else {
            Log.v("No userInfo found in notification")
            callback.onResponseReceived(nil)

            return
    }

    if let json = msg.json {
        callback.onResponseReceived(Response_Base(json: json.dictionary))
    } else {
        Log.v("Could not parse msg")
        callback.onResponseReceived(nil)
    }
}

问题是我不知道为什么程序总是返回“No userInfo found in notification”。谁能帮帮我?

我的 userInfo 输出是:

[AnyHashable("{ \n \"req_token\":\"test\",\n \"conv_token\":\"test\",\n \"ts\":\"2017-04- 05 12:00:00.123\",\n \"code\":0,\n \"message\":\"ACCESSO CONCEDIDO\",\n \"auth_token\":\"jakldsfjalkdfj\",\n \"response\":{ \n \"type\":\"test\",\n \"data\":{ \n \"destinos\":[ \n { \n \"idDestino\": \"1\",\n \"desDestino\":\"ASUNCION\"\n },\n { \n \"idDestino\":\"2\",\n \"desDestino\":\"迈阿密\"\n }\n ]\n }\n }\n}"): ""]

【问题讨论】:

  • 第一个错误在我看来[message.string! : String()] 是一个字典,其中消息字符串作为键, 并且空字符串作为值。这可能不是你想要的。
  • 抱歉...但是当我尝试不陷入“通知中未找到用户信息”时,有什么方法可以帮助我?
  • 将您的 guard let userInfo.... 声明分成两部分,以便您知道这是用户信息问题还是“idDestino”问题。

标签: swift notifications notificationcenter userinfo


【解决方案1】:

好的...我解决了我的问题...感谢@martin R

问题出在这一行:

 bus.post(name: .DestinationRespReceived, userInfo: [message.string! : String()])

我没有意识到 key 是我的 userInfo 的第一个元素,第二个元素是 value。所以我更正了我的 userInfo 位置的元素,如下所示:

bus.post(name: .DestinationRespReceived, userInfo: ["msg" : message.string!])

所以...在我的函数中 responseReceived 现在是正确的:

 guard let userInfo = notification.userInfo, let msg = userInfo["msg"] as? String else {

            Log.v("No userInfo found in notification")
            callback.onResponseReceived(nil)

            return
    }

谢谢!

【讨论】:

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