(iOS 10,Swift 3)从CloudKit通知中读取`userInfo`字典:如何将`[AnyHashable:Any]`转换为`[String:NSObject]`?

Ant*_*y C 6 notifications cloudkit swift3 ios10

背景

我正在尝试userInfoapplication:didReceiveRemoteNotification:userInfo:fetchCompletionHandler我的app委托中加载字典.

然后我需要转换userInfo[AnyHashable:Any],[String:NSObject]所以我可以在CloudKit中使用它CKNotification:fromRemoteNotificationDictionary.

当我做:

let ui = userInfo as! [String : NSObject]
Run Code Online (Sandbox Code Playgroud)

我收到错误:

'[AnyHashable:Any]' is not convertible to '[String:NSObject]'
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法转换userInfo为适当的类型,或者我是在一个完全错误的轨道?

Leo*_*bus 5

你只需要先将它施放NSDictionary,然后你可以将其投射到[String: NSObject].

试试这样:

CKNotification(fromRemoteNotificationDictionary: userInfo as NSDictionary as! [String: NSObject])
Run Code Online (Sandbox Code Playgroud)