Swift didReceiveRemoteNotification userInfo 无法获取值 [英] Swift didReceiveRemoteNotification userInfo can't get value

查看:91
本文介绍了Swift didReceiveRemoteNotification userInfo 无法获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用推送通知,并有这个功能:

I use push notification, and have this func:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if let msg = userInfo["msg"] as? NSObject {
        println(msg)
    }
    GCMService.sharedInstance().appDidReceiveMessage(userInfo);
    NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
        userInfo: userInfo)
}

这打印了我:

{"model":"que","data":{"id":101,"vehicle":{"license_plate":"test","taxi":{"id":1,"logo":"/media/logos/mega_CxRn739.png.75x75_q85_crop.png","name":"Mega","city":"Novi Pazar","country":"Srbija"},"label":"A01"}}}

// prettify:
{
    "model": "que",
    "data": {
        "id": 101,
        "vehicle": {
            "license_plate": "test",
            "taxi": {
                "id": 1,
                "logo": "/media/logos/mega_CxRn739.png.75x75_q85_crop.png",
                "name": "Mega",
                "city": "Novi Pazar",
                "country": "Srbija"
            },
            "label": "A01"
        }
    }
}

现在当我使用:

if let msg = userInfo["msg"] as? NSObject {
    println(msg["model"])
    println(msg["data"])
}

我无法构建:

'NSObject' does not have a member named 'subscript'

我怎样才能让它工作?我需要在这之后获得所有财产,但不能做第一步.

How can i get this to work? I need to get all propertise after this, but can't do first step.

图片:

修复:

if let msg = userInfo["msg"] as? String {
    if let data = msg.dataUsingEncoding(NSUTF8StringEncoding) {
        if let jsonObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(), error: nil) {
            var data = JSON(jsonObject!)
            println(data["data"])
        }
    }
}

如果有人对此修复有任何建议,请告诉我..谢谢.

If anyone have suggestion on this fix, tell me.. Thanks.

推荐答案

if let msg = userInfo["msg"] as? String {
    if let data = msg.dataUsingEncoding(NSUTF8StringEncoding) {
        if let jsonObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(), error: nil) {
            var data = JSON(jsonObject!)
            println(data["data"])
        }
    }
}

如果有人对此修复有任何建议,请告诉我..谢谢.

If anyone have suggestion on this fix, tell me.. Thanks.

这篇关于Swift didReceiveRemoteNotification userInfo 无法获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆