Rac*_*hel 4 foreach xcode set swift swiftui
当尝试在另一个视图中显示我从列表中选择的项目时,我收到错误:“通用结构‘ForEach’要求‘Set’符合‘RandomAccessCollection’”。
import SwiftUI
struct ExSetView: View {
@Environment(\.managedObjectContext) var viewContext
@Environment(\.dismiss) var dismiss
@State var selectedItems = Set<Exercise>()
var body: some View {
NavigationView {
VStack (alignment: .leading) {
Text("Set Count: \(selectedItems.count)")
ForEach(selectedItems) { e in
NavigationLink(
destination: ExSetInputView(exset: e),
label: {
Text(e.exercisename)
}
)}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误就上线了ForEach(selectedItems) { e in
我不知道如何解决这个问题。最终我只想通过其executionname 属性作为导航链接显示所选项目。还有另一种方法不使用 For Each 吗?问题是因为它是一个集合而不是一个数组吗?任何帮助将不胜感激!!!!
vad*_*ian 15
ForEach需要有序数据源,aSet根据定义是无序的。
一个简单的解决方案是对集合进行排序,例如exercisename
ForEach(selectedItems.sorted{$0.exercisename < $1.exercisename})
Run Code Online (Sandbox Code Playgroud)
的结果sorted是一个符合要求的数组'RandomAccessCollection。
| 归档时间: |
|
| 查看次数: |
6763 次 |
| 最近记录: |