I am trying to extract icon value from userInfo dictionary of remote notification. I read a lot of posts and found a solution to my problem, but I am very unsatisfied!
So here is my data structure (I deleted useless lines): { aps = { badge = 7 } }
To extract this number "7" from my userInfo , I would like to do the following:
self.updateAppIcon(userInfo["aps"]["badge"] as? Int)
But of course, I get the following error:
Swift: '(NSObject, AnyObject)' does not have a member named 'subscript'
If I am not mistaken, this is because [] returns AnyObject, which cannot be interpreted as another dictionary.
A working solution would be to do the following:
func handleRemoteNotifiation(userInfo: [NSObject : AnyObject]) { if let aps: AnyObject = userInfo["aps"] { if let apsDict = aps as? [String : AnyObject]{ if let badge: AnyObject = apsDict["badge"] { self.updateAppIconBadgeNumber(badge as? Int) } } } } func updateAppIconBadgeNumber(number: Int?) {
But seriously ... can I do it more sexually? fewer lines, fewer if clauses, fewer throws, etc.? This is a "code-based" solution to a simple thing.
thanks
source share