fanfan
fanfan

Reputation: 514

Swift: 'Dictionary' is not identical to 'Dictionary<Key, Value>'

I'm trying to make a Dictionary in Swift that maps a MKPointAnnotation to an Event, where an Event is a custom class.

I have instantiated the Dictionary as such:

var annotations: Dictionary = [MKPointAnnotation : Event]()

And am attempting to add to it by:

annotations[annotation as MKPointAnnotation] = event as Event

Which according to the Swift documentation is a valid method. I've also tried using:

annotations.updateValue(event, forKey: annotation)

However both of these methods give the error:

'Dictionary' is not identical to 'Dictionary<Key, Value>'

Any ideas why?

Upvotes: 1

Views: 244

Answers (1)

Chris
Chris

Reputation: 40623

You're up-casting to a plain old Dictionary when you want a Dictionary<MKPointAnnotation, Event>.

Change the following line:

var annotations: Dictionary = [MKPointAnnotation : Event]()

to:

var annotations = [MKPointAnnotation : Event]()

Then do an alt-click on 'annotations' and see if it has inferred the correct type, which should be Dictionary<MKPointAnnotation, Event> instead of the non-generic Dictionary.

Upvotes: 4

Related Questions