我使用推送通知,並將本FUNC:斯威夫特didReceiveRemoteNotification用戶信息不能得到價值
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"])
}
}
}
如果任何人有此修復程序的建議,告訴我..謝謝。
嗯...也許把'msg'作爲'NSDictionary'並使用'valueForKey:'來獲取內容? – duci9y
什麼都沒有發生,仍然是一樣的:( –
什麼都沒有出來到控制檯?甚至不是一個時間戳?嘗試'println(「這應該工作:\(msg.whatever())」)'?你確定代碼是甚至執行? – duci9y