背景
我正在尝试在我的应用程序代理中从application:didReceiveRemoteNotification:userInfo:fetchCompletionHandler加载userInfo字典。
然后,我需要将userInfo从[AnyHashable:Any]转换为[String:NSObject],这样我就可以在CloudKit的CKNotification:fromRemoteNotificationDictionary中使用它。
问题
当我这样做的时候:
let ui = userInfo as! [String : NSObject]我得到了错误:
'[AnyHashable:Any]' is not convertible to '[String:NSObject]'有没有更好的方法将userInfo转换为合适的类型,或者我走在一个完全错误的轨道上?
发布于 2016-08-30 12:59:23
您只需要先将其强制转换为NSDictionary,然后再将其强制转换为[String: NSObject]。
试着这样做:
CKNotification(fromRemoteNotificationDictionary: userInfo as NSDictionary as! [String: NSObject])发布于 2016-11-24 17:19:56
NSString.localizedStringWithFormat("%@",(notification.userInfo as! [String: AnyObject] as! [String : NSObject])["theKeyValue"]!)我就是这样弄到这根线的。请随时更正它。
发布于 2016-09-05 16:07:24
正如建议的那样,移除所有强制转换并使用userInfo就足以让Swift 3正确地处理这件事。
https://stackoverflow.com/questions/39215160
复制相似问题