Update: The issue is probably caused by the preview code in Xcode.
I've airdropped my ContentView to my classmate's MacBook Air running macOS Sonoma and Xcode 15. The same issue occurred.
But after replacing the preview code:
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
with the preview code used in Xcode 15:
#Preview {
ContentView()
.modelContainer(for: Item.self, inMemory: true)
}
The issue disappeared. Everything works well.
The problem is that the latter code isn't available for Xcode 14 or earlier version. I wonder if there're any workarounds for this?
Thank you all for viewing or replying!! =)