Reputation: 1085
I updated to Swift 3 and I'm using Google Analytics in my project. Originally I had this line of code working:
tracker.send(GAIDictionaryBuilder.createScreenView().build() as [NSObject : AnyObject])
After the Swift 3 conversion tool finished, it changed it to this:
tracker?.send(GAIDictionaryBuilder.createScreenView().build() as [AnyHashable: Any])
I'm getting the error:
Cannot convert value of type NSMutableDictionary! to type [AnyHashable:Any] in coercion
I can get rid of this error by changing the line to:
tracker?.send(GAIDictionaryBuilder.createScreenView().build() as? [AnyHashable: Any])
However this gives me the warning in the title. Does anyone know why this is happening? Is this a bug in Swift 3?
Upvotes: 1
Views: 1301
Reputation: 2628
I solved it by this (safer) way:
if let eventTracker = GAIDictionaryBuilder.createScreenView().build() {
var eventTrackerSwift: [AnyHashable: Any] = [:]
for item in eventTracker {
if let key = item.key as? AnyHashable {
eventTrackerSwift[key] = item.value
}
}
tracker.send(eventTrackerSwift)
}
Upvotes: 0
Reputation: 47876
I'm not sure if Swift Team (or Apple?) thinks that is a bug. But in fact, Swift 3 runtime can convert value of type NSMutableDictionary!
to type [AnyHashable:Any]
using as!
.
To suppress the warning, please try this:
tracker?.send(GAIDictionaryBuilder.createScreenView().build() as NSDictionary as! [AnyHashable : Any])
Upvotes: 2