首页 > 解决方案 > 无法将 Dictionary 类型的值分配给 LazyMapCollection

问题描述

我正在开发一个应用程序,我对 Swift 比较陌生,我尝试使用以下代码使用 answer 变量的键初始化 answerKeys,但它显示错误。

无法将类型 'Dictionary<IntPoint, String>.Keys' 的值分配给类型 'LazyMapCollection<Dictionary<IntPoint, String>, IntPoint>'(又名 'LazyMapSequence<Dictionary<IntPoint, String>, IntPoint>')

我已经浏览了文档,但无法解决这个问题。

var answer:[IntPoint:String] = [:]
var answerKeys:LazyMapCollection<Dictionary<IntPoint,String>,IntPoint>

init() {
    answerKeys = answer.keys
}

标签: swift

解决方案


可能是在早期的 Swift 版本中Dictionary.keys返回了 a 。LazyMapCollection在 Swift 5 中,从文档Dictionary<Key, Value>.Keys中可以看出,在您的情况下

var answerKeys: Dictionary<IntPoint, String>.Keys

但请注意,您始终可以answer.keys在代码中访问,而不是将其分配给单独的属性。


推荐阅读