swift - 无法将 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 版本中Dictionary.keys
返回了 a 。LazyMapCollection
在 Swift 5 中,从文档Dictionary<Key, Value>.Keys
中可以看出,在您的情况下
var answerKeys: Dictionary<IntPoint, String>.Keys
但请注意,您始终可以answer.keys
在代码中访问,而不是将其分配给单独的属性。
推荐阅读
- javascript - RxJS - 哪个运算符可以结合最近 N 个事件的历史?
- facebook - 为什么即使在应用审核后也无法获取 Facebook 事件?
- string - 串行数据从连续字符串读取并解析为变量(Arduino)
- c# - 知道我将使用syncfusion来制作我的UI控件,学习Windows窗体或WPF会更好吗?
- c# - 如何从 UserControl 实时获取结果?
- amazon-web-services - Terraform 中的相对路径
- php - SOAP:过程“helloRequest”不存在
- html - 水平滚动条 - 面板内无法容纳信息?
- codeigniter - 在代码点火器中更改登录用户的密码
- java - 尝试在排序数组中插入元素时出错