Reputation: 117
I had my files conveted to Swift3, one dictionary was like
var Detail: [String:[String:AnyObject]] = [
"0":["0":["number":0,"imageName":"40"]],
"1":["0":["number":1,"imageName":"3setting"],"1":["number":1,"imageName":"3private"],"2":["number":1,"imageName":"3currency"]],
"2":["0":["number":1,"imageName":"3favourite"],"1":["number":1,"imageName":"3favourite"]],
"3":["0":["number":1],"1":["number":1]],
]
I works perfectly in the past, but today it reminded
Contextual type 'AnyObject' cannot be used with dictionary literal
But why it doesn't work now? Why this happeded and how to solve it?
Upvotes: 0
Views: 84
Reputation: 536047
Write [String:Any]
instead of [String:AnyObject]
and you'll be fine.
As for "what happened": automatic bridging went away (e.g. between Int and NSNumber, or String and NSString). So a literal dictionary like ["imageName":"3setting"]
is inferred as a [String:String]
, and cannot be assigned where a [String:AnyObject]
is expected — because a String is not an AnyObject. But a String is certainly an Any, because everything is an Any.
You could alternatively work around this by writing ["number":1 as NSNumber, "imageName":"3setting" as NSString]
(because an NSNumber or an NSString is an AnyObject), but there seems little point in doing that here. In the general case, a dictionary is an [AnyHashable:Any]
now, and you should use that as the catch-all type; AnyObject is basically going away, slowly but surely.
Upvotes: 3