【问题标题】:Can't assign value of type Dictionary to LazyMapCollection无法将 Dictionary 类型的值分配给 LazyMapCollection
【发布时间】:2019-11-20 08:21:09
【问题描述】:

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

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

我浏览了文档,但无法解决此问题。

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

init() {
    answerKeys = answer.keys
}

【问题讨论】:

    标签: swift


    【解决方案1】:

    可能是Dictionary.keys 在早期的 Swift 版本中返回了 LazyMapCollection。在 Swift 5 中,它是 Dictionary&lt;Key, Value&gt;.Keys,从 documentation 可以看出,在您的情况下

    var answerKeys: Dictionary<IntPoint, String>.Keys
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      • 2019-12-03
      • 2017-08-07
      • 2021-03-08
      相关资源
      最近更新 更多