Anthony C
Anthony C

Reputation: 1990

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

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?

Upvotes: 6

Views: 4970

Answers (3)

love2script12
love2script12

Reputation: 210

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

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

Upvotes: 0

Fabrizio Bartolomucci
Fabrizio Bartolomucci

Reputation: 4958

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

Upvotes: -1

Leo Dabus
Leo Dabus

Reputation: 236370

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])

Upvotes: 6

Related Questions