2016-09-27 45 views
0

我試圖獲得「alert」的值,但沒有非常幸運。iOS Swift獲取懶惰地圖集合值

LazyMapCollection<Dictionary<AnyHashable, Any>, AnyHashable>(_base: [AnyHashable("google.c.a.e"): 1, AnyHashable("google.c.a.ts"): 1475009164, AnyHashable("google.c.a.udt"): 0, AnyHashable("gcm.n.e"): 1, AnyHashable("aps"): { 
    alert = "test message"; 
}, AnyHashable("google.c.a.c_id"): 9039575328704479116, AnyHashable("gcm.message_id"): 0:1475009164740581%df6f7f01df6f7f01], _transform: (Function)) 

這是代碼是一個使用,但它不能正常工作

if (userInfo["aps"] != nil) { 
        let msg = (userInfo["alert"] as? String)! 
        let alert1 = UIAlertController(title: "Notification", message: msg, preferredStyle: .alert) 
        alert1.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) -> Void in 
        })) 
        self.window?.rootViewController?.present(alert1, animated: true, completion: nil) 
        completionHandler(.newData) 
       } 

我試圖顯示與該值警報的消息的本地通知。任何想法,將不勝感激。謝謝

+1

什麼是「不正常」呢?它做什麼,你期望它做什麼?這個'LazyMapCollection'與它下面的代碼有什麼關係? –

+0

我試圖在通知中顯示「alert」的值,即「test message」。這行代碼拋出一個異常,讓msg =(userInfo [「alert」] as String)! –

回答

0

這是我工作的解決方案

if (userInfo["aps"] != nil) { 
        if let notification = userInfo["aps"] as? NSDictionary, 
         let alert = notification["alert"] as? String { 
         let alert1 = UIAlertController(title: "Notification", message: alert, preferredStyle: .alert) 
         alert1.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) -> Void in 
         })) 
         self.window?.rootViewController?.present(alert1, animated: true, completion: nil) 
         completionHandler(.newData) 

        } 
       }