(iOS 10,Swift 3)从CloudKit通知中读取`userInfo`字典:如何将`[AnyHashable:Any]`强制转换为`[String:NSObject]`? [英] (iOS 10, Swift 3) Reading `userInfo` dictionary from a CloudKit notification: How do I cast `[AnyHashable : Any]` to `[String : NSObject]`?
问题描述
我正在尝试从应用程序加载
在我的应用程序委托中。 userInfo
字典: didReceiveRemoteNotification:userInfo:fetchCompletionHandler
I'm trying to load the userInfo
dictionary from application:didReceiveRemoteNotification:userInfo:fetchCompletionHandler
in my app delegate.
然后我需要转换 userInfo
从 [AnyHashable:Any]
到 [String:NSObject]
,所以我可以在CloudKit的<$ c $中使用它c> CKNotification:fromRemoteNotificationDictionary 。
I then need to cast userInfo
from [AnyHashable:Any]
to [String:NSObject]
so I can use it in CloudKit's CKNotification:fromRemoteNotificationDictionary
.
我这样做时:
let ui = userInfo as! [String : NSObject]
我收到错误:
'[AnyHashable:Any]' is not convertible to '[String:NSObject]'
是否有更好的方法将 userInfo
转换为适当的类型,还是我走错了路?
Is there a better way to convert userInfo
to the appropriate type, or am I on a completely wrong track?
推荐答案
您只需先将其强制转换为 NSDictionary
,然后将其强制转换为 [String:NSObject]
。
You just need to cast it first to NSDictionary
and then you can cast it to [String: NSObject]
.
尝试像这样:
CKNotification(fromRemoteNotificationDictionary: userInfo as NSDictionary as! [String: NSObject])
这篇关于(iOS 10,Swift 3)从CloudKit通知中读取`userInfo`字典:如何将`[AnyHashable:Any]`强制转换为`[String:NSObject]`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!