I am trying to create a Multiplatform Document Based app using SwiftUI.
I have a List which the user can reorder by dragging Items using .onMove. It works fine in macOS but crashes iOS every time with the above message alongside @main
The relevant code is-
struct ContentView: View {
…
List {
ForEach(Array(document.journey.items.enumerated()), id: \.1.id) { (index, item) in
Section {
HStack {
Text("\(index+1)")
…
Text("\(item.address)"
…
} // End HStack
.background(
Capsule()
.fill((selection.contains(item.id) ? Color.red : Color.blue))
.onTapGesture(perform: {
if selection.contains(item.id) {
selection.remove(item.id)
} else {
selection.insert(item.id)
}
})
)
.font(.headline)
.foregroundColor(.white)
} // End Section
…
} // End ForEach
.onDelete(perform: delete)
.onMove { offsets, toOffset in
document.moveItems(offsets: offsets, toOffset: toOffset, undoManager: undoManager)
}
} // End List
////////
final class MultiStopDocument: ReferenceFileDocument {
…
func moveItems(offsets: IndexSet, toOffset: Int, undoManager: UndoManager? = nil) {
let oldItems = journey.items
// BREAK POINT
print("GOT to First")
withAnimation {
journey.items.move(fromOffsets: offsets, toOffset: toOffset)
}
print("GOT to Second")
#if os(macOS)
undoManager?.registerUndo(withTarget: self) { doc in
doc.replaceItems(with: oldItems, undoManager: undoManager)
}
#endif
}
///////////
import SwiftUI
@main - Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1af84999c)
struct MultiStopApp: App {
Console macOS stepping :-
GOT to First GOT to Second
Console iOS stepping :-
2023-06-07 12:03:16.869439+0100 MultiStop[27341:6172177] -[UITextEffectsWindow _accessibilityFindSubviewDescendant:]: unrecognized selector sent to instance 0x112037400 GOT to First 2023-06-07 12:03:52.083774+0100 MultiStop[27341:6172433] XPC connection interrupted GOT to Second THEN CRASH
Update-
I managed to get a lab appointment with WWDC. Thanks very much to the Apple people who helped me. It took a large part of the 30 minute appointment but they found a solution.
Further to the code that I posted above the List was enclosed in a Section (you can see where I commented // End Section above). Removing the Section made everything work.
I still don't know why it worked on macOS but at the moment I am just happy that it is working on iOS as well.
Thanks for taking the time to look, those of you that did, and maybe I could be a pointer for others who keep searching like I had been.