You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create an Xcode SwiftUI iOS app project, create a new SwiftUI view file HierarchyList.swift, paste the following and then preview to see Abort: trap 6 or run to see many more errors in Xcode
// https://gist.github.com/zntfdr/9d94cac84235f04f021cb6c7e8c1a1c5// Original article here: https://www.fivestars.blog/code/swiftui-hierarchy-list.htmlimportSwiftUIstructFileItem: Identifiable {
varname: Stringvarchildren: [FileItem]?
letid = UUID()
staticletspmData: [FileItem] = [
FileItem(name: ".gitignore"),
FileItem(name: "Package.swift"),
FileItem(name: "README.md"),
FileItem(name: "Sources", children: [
FileItem(name: "fivestars", children: [
FileItem(name: "main.swift")
]),
]),
FileItem(name: "Tests", children: [
FileItem(name: "fivestarsTests", children: [
FileItem(name: "fivestarsTests.swift"),
FileItem(name: "XCTestManifests.swift"),
]),
FileItem(name: "LinuxMain.swift")
])
]
}
// This takes a binding to array of recursive item structpublicstructHierarchyList<Data, RowContent>: ViewwhereData: MutableCollection, Data: RandomAccessCollection, Data.Index: Hashable, Data.Element: Identifiable, RowContent: View {
privateletrecursiveView: RecursiveView<Data, RowContent>
publicinit(@Bindingdata: Data, children: KeyPath<Binding<Data.Element>, Binding<Data?>>, rowContent: @escaping (Binding<Data.Element>) -> RowContent) {
recursiveView = RecursiveView(data: $data, children: children, rowContent: rowContent)
}
publicvarbody: someView {
List {
recursiveView
}
}
}
// !!!// this version uses ForEach w/ SE-0293 @Binding closure parameter.// this version is preferred but doesn't compile as of Xcode Version 13.0 beta 4 (13A5201i)//private struct RecursiveView__<Data, RowContent>: View where Data: MutableCollection, Data: RandomAccessCollection, Data.Index: Hashable, Data.Element: Identifiable, RowContent: View {// @Binding var data: Data// let children: KeyPath<Binding<Data.Element>, Binding<Data?>>// let rowContent: (Binding<Data.Element>) -> RowContent//// var body: some View {// // https://forums.swift.org/t/xcode-abort-trap-6-and-non-sensical-compile-error-maybe-related-to-se-0293/50907/2// // Use SE-0293 @Binding property wrapper closure parameter:// ForEach($data) { $item in // !!! compile error: Cannot convert value of type 'Binding<Data>.Element' (aka 'Binding<Data.Element>') to expected argument type 'Binding<Data.Element>'// if let unwrappedBindingToChildren = Binding($item[keyPath: children]) {// DisclosureGroup(content: {// RecursiveView(data: unwrappedBindingToChildren, children: children, rowContent: rowContent)// }, label: {// rowContent($item)// })// } else {// rowContent($item)// }// }// }//}// the above do not compile, so use ForEach(.indices, ...) insteadprivatestructRecursiveView<Data, RowContent>: ViewwhereData: MutableCollection, Data: RandomAccessCollection, Data.Index: Hashable, Data.Element: Identifiable, RowContent: View {
@Bindingvardata: Dataletchildren: KeyPath<Binding<Data.Element>, Binding<Data?>>
letrowContent: (Binding<Data.Element>) -> RowContentvarbody: someView {
ForEach(data.indices, id: \.self) { indexinifletbindingToChildren = Binding($data[index][keyPath: children]) {
DisclosureGroup(content: {
RecursiveView(data: bindingToChildren, children: children, rowContent: rowContent)
}, label: {
rowContent($data[index])
})
} else {
rowContent($data[index])
}
}
}
}
structHierarchyListDemo: View {
@Stateprivatevardata = FileItem.spmDatavarbody: someView {
// rowContent receive a binding to data item, enabling mutate on the itemHierarchyList($data: $data, children: \.children, rowContent: { $iteminTextField("Edit", text: $item.name) })
}
}
structHierarchyList_Previews: PreviewProvider {
staticvarpreviews: someView {
HierarchyListDemo()
}
}
The issue here is the wrapped parameter in the initializer; the workaround is to use the wrapper/projection type directly, or use a local property wrapper instead.
Environment
Xcode Version 13.0 beta 4 (13A5201i)
Additional Detail from JIRA
md5: ec63ff96d1fef552ade6af181f1d18f8
Issue Description:
Create an Xcode SwiftUI iOS app project, create a new SwiftUI view file HierarchyList.swift, paste the following and then preview to see Abort: trap 6 or run to see many more errors in Xcode
See https://forums.swift.org/t/xcode-abort-trap-6-and-non-sensical-compile-error-maybe-related-to-se-0293/50907
The text was updated successfully, but these errors were encountered: