(iOS 10, Swift 3) Reading `userInfo` dictionary from a CloudKit notification: How do I cast `[AnyHashable : Any]` to `[String : NSObject]`?

痞子三分冷 提交于 2019-12-07 02:56:55

问题


Background

I'm trying to load the userInfo dictionary from application:didReceiveRemoteNotification:userInfo:fetchCompletionHandler in my app delegate.

I then need to cast userInfo from [AnyHashable:Any] to [String:NSObject] so I can use it in CloudKit's CKNotification:fromRemoteNotificationDictionary.

Question

When I do:

let ui = userInfo as! [String : NSObject]

I get the error:

'[AnyHashable:Any]' is not convertible to '[String:NSObject]'

Is there a better way to convert userInfo to the appropriate type, or am I on a completely wrong track?


回答1:


You just need to cast it first to NSDictionary and then you can cast it to [String: NSObject].

Try like this:

CKNotification(fromRemoteNotificationDictionary: userInfo as NSDictionary as! [String: NSObject])



回答2:


NSString.localizedStringWithFormat("%@",(notification.userInfo as! [String: AnyObject] as! [String : NSObject])["theKeyValue"]!)

This is how I got the string. Feel free to correct it.




回答3:


As suggested, it is enough to take away all casts and use userInfo as such for Swift 3 to handle the thing correctly.



来源:https://stackoverflow.com/questions/39215160/ios-10-swift-3-reading-userinfo-dictionary-from-a-cloudkit-notification-ho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!