我使用推送通知,并拥有此功能:
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'
我怎样才能让它发挥作用?我需要在此之后获得所有财产,但不能做第一步。
图像:
FIX :
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"])
}
}
}
如果有人对此修复有任何建议,请告诉我..谢谢。
答案 0 :(得分:3)
"file://android_assest/*"
如果有人对此修复有任何建议,请告诉我..谢谢。
答案 1 :(得分:2)
有效负载中的相关对象是字典,因此将msg
的值转换为更具体的
if let msg = userInfo["msg"] as? [String:AnyObject] {
println(msg["model"])
println(msg["data"])
}
修改强>
可能是msg
的对象只是一个JSON字符串。
试试这个:
if let msg = userInfo["msg"] as? String {
println("yeah, it's a string")
if let data = msg.dataUsingEncoding(NSUTF8StringEncoding) {
if let jsonObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(), error: nil) {
println(jsonObject["model"])
println(jsonObject["data"])
}
}
}