swift - 如何从通知中访问 userInfo 属性?

标签 swift notifications notificationcenter userinfo

我调用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":"test",
   "conv_token":"test",
   "ts":"2017-04-05 12:00:00.123",
   "code":0,
   "message":"ACCESO CONCEDIDO",
   "auth_token":"test",
   "response":{  
      "type":"test",
      "data":{  
         "destinos":[  
            {  
               "idDestino":"1",
               "desDestino":"ASUNCION"
            },
            {  
               "idDestino":"2",
               "desDestino":"MIAMI"
            }
         ]
      }
   }
}

This is my post method declaration:

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

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

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)
    }
}

问题是我不知道为什么程序总是返回“在通知中找不到 userInfo”。有人可以帮我吗?

我的用户信息输出是:

[AnyHashable("{ \n \"req_token\":\"test\",\n \"conv_token\":\"test\",\n \"ts\":\"2017-04-05 12:00:00.123\",\n \"code\":0,\n \"message\":\"ACCESO 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\":\"MIAMI\"\n }\n ]\n }\n }\n}"): ""]

最佳答案

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

问题出在这一行:

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

我没有意识到键是我的 userInfo 的第一个元素,第二个元素是值。所以我像这样更正了 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
    }

谢谢!

关于swift - 如何从通知中访问 userInfo 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45198956/

相关文章:

Swift-根据坐标和时间改变星图 View

php - 用于 PHP 和 MySQL 应用程序的 Windows 弹出气球

ios - 在 .c 文件中发布通知

ios - 在通知中心永久显示数据的应用程序

ios - 在多个平台上设置使用 Realm 的 Xcode 框架

ios - ARC 在解除分配之前是否将其引用类型实例属性设置为 nil?

notifications - 无法在 ios 10 通知中调试通知内容扩展

ios - 打电话回来后从通知中心收到通知

swift - NotificationCenter 将 swift 3 迁移到 swift 4.2 的问题

ios - BindMemory 迁移 Swfit 3