2015-07-04 23 views
1

我使用推送通知,並將本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' 

我如何得到這個工作?我需要在此之後獲得所有的信息,但不能做第一步。

圖片: enter image description here

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

嗯...也許把'msg'作爲'NSDictionary'並使用'valueForKey:'來獲取內容? – duci9y

+0

什麼都沒有發生,仍然是一樣的:( –

+0

什麼都沒有出來到控制檯?甚至不是一個時間戳?嘗試'println(「這應該工作:\(msg.whatever())」)'?你確定代碼是甚至執行? – duci9y

回答

3
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"]) 
     } 
    } 
} 

如果任何人有此修復程序的建議,告訴我..謝謝。

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"]) 
     } 
    } 
} 
+0

現在我沒有得到錯誤,但沒有打印。爲什麼? –

+0

確實[NSObject:AnyObject]有效嗎? – vadian

+0

不,沒有打印.. –