如何使用 SwiftUI ForEach 与字典 [ customEnum : customStrut ] - 得到"conform to 'RandomAccessCollection'"错误



如何纠正失败构建的这段代码? 基本上想使用 ForEach 遍历基于 [ customEnum : customStrut ] 的字典。

否则,如果这是有问题的,那么另一种实现 SwiftUI 支持的方法?

错误

在"ForEach"上引用初始值设定项"init(_:id:content:)"需要 那 '[GCFilterViewOptions.FilterOptions : GCFilterViewOptions.FilterOptionValues]' 符合 "随机访问集合">

键入 '(键: GCFilterViewOptions.FilterOptions, value: GCFilterViewOptions.FilterOptionValues)' 不能符合 'Hashable'; 只有结构/枚举/类类型可以符合协议

法典

import SwiftUI
struct GCFilterViewOptions: View {
enum FilterOptions {
case NewLine
case Comma
case Space
}
struct FilterOptionValues {
var title : String
var selected : Bool
}
var filterSelections : [FilterOptions : FilterOptionValues] = [
FilterOptions.NewLine : FilterOptionValues(title: "New Line", selected: true),
FilterOptions.Comma : FilterOptionValues(title: "Comma", selected: true),
FilterOptions.Space : FilterOptionValues(title: "Space", selected: false)
]
var body : some View {
HStack {
ForEach(filterSelections, id:.self) { filterOption in.  // ** ERRORS HERE **
Text("TBD")
// Will be putting checkboxes here - i.e. so can chose which ones 
}
}
}
}

由于状态字典不是"随机访问"功能的集合,因此不能直接在ForEach中使用,因此这是可能的方法

HStack {
ForEach(Array(filterSelections.keys.enumerated()), id:.element) { _, key in
Text("TBD (self.filterSelections[key]?.title ?? "")")
// Will be putting checkboxes here - i.e. so can chose which ones
}
}

ForEach 循环仅支持支持 RandomAccess 的数据结构(Apple 关于 RandomAccessCollection 出色的文档)。

如果你真的不需要字典结构,你可以使用一个简单的结构。这基本上与您的相同,但枚举嵌套在其中并且是一个属性:

struct FilterOption {
enum FilterOptionType {
case newLine
case comma
case space
}
var type: FilterOptionType
var title: String
var selected: Bool
}
var filterSelection: [FilterOption] = [
FilterOption(type: .newLine, title: "New line", selected: true),
FilterOption(type: .comma, title: "Comma", selected: false),
FilterOption(type: .space, title: "Space", selected: true),
]

如果你想让你使用字典(例如断言选项只列出一次),你应该保留你现在的数据结构,只使用字典键来访问元素。请记住,字典不是有序的,如果你想要一个稳定的顺序,你应该对它进行排序(例如使用键的 rawValue):

enum FilterOption: String {
case newLine
case comma
case space
}
struct FilterOptionValue {
var title: String
var selected: Bool
}
var filterSelection: [FilterOption: FilterOptionValue] = [
.newLine: FilterOptionValue(title: "New Line", selected: true),
.comma: FilterOptionValue(title: "Comma", selected: true),
.space: FilterOptionValue(title: "Space", selected: false)
]
// Guarantees a stable print order
for option in filterSelection.keys.sorted(by: { $0.rawValue < $1.rawValue }) {
let optionValue = filterSelection[key]!
print(option, optionValue)
}

请注意,在 Swift 中,枚举情况与属性一样是小写的。只有类型以大写字母开头(newLine,而不是NewLine)。

此外,枚举类型名称应该是单数的,因为在实例化时它们只代表一个选项。使用FilterOption而不是FilterOptions

对于应该是单数的filterSelections也是如此,因为选择已经包含多个元素。

最新更新