繁体   English   中英

Swift:“字典”与“字典”不同 <Key, Value> &#39;

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

我正在尝试在Swift中制作一个Dictionary ,该DictionaryMKPointAnnotation映射到Event ,其中Event是一个自定义类。

我已经实例化了Dictionary

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

并尝试通过以下方式添加它:

annotations[annotation as MKPointAnnotation] = event as Event

根据Swift文档,哪个是有效方法。 我也尝试过使用:

annotations.updateValue(event, forKey: annotation)

但是,这两种方法都会产生错误:

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

有什么想法吗?

当您需要Dictionary<MKPointAnnotation, Event>时,您正在向上转换到普通的旧Dictionary

更改以下行:

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

至:

var annotations = [MKPointAnnotation : Event]()

然后按Alt键单击“注释”,看看它是否推断出正确的类型,应为Dictionary<MKPointAnnotation, Event>而不是非通用Dictionary

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM