I have a simple enum with Int raw values:
enum AccountType: Int, Codable, CaseIterable {
case creditAccountReceivable = 1,
creditAccountPayable = 2,
nominal = 3
}
In my model class I have a field:
var type: AccountType.RawValue
I use the following filter in my View:
@Query(
filter: #Predicate { $0.type == AccountType.nominal.rawValue },
sort: [SortDescriptor(\Account.group), SortDescriptor(\Account.accountName)])
private var nominalAccounts: [Account]
This causes a build failure with 3 errors:
'AccountType.Type' cannot conform to 'Encodable'
'AccountType.Type' cannot conform to 'Decodable'
Key path cannot refer to enum case 'nominal'
The only way I have been able to make it work is by substituting an Int literal in place of the enum raw value: filter: #Predicate { $0.type == 3 }
The other workaround I have used is reading all enitities and filtering using a computed variable.
Post
Replies
Boosts
Views
Activity
My iOS 15.5 SwiftUI app crashes on iPhone 11 simulator. After reloading the app the CoreData entities have been successfully deleted. The crash occurs sometime between the last executable line of code in a delete confirmation alert which reloads the list of items in the view without error. Execution never reaches the body property of the view.
I can't think of any other way to determine what is causing the error and I cannot decode the crash report to help me! Any ideas would be much appreciated as the app is otherwise working great!!
Here is the crash report:
Crash report
@ObservedObject var vm: CourseListViewModel
private var isPreview: Bool
@State private var refreshDataOnReappear = false
@State private var showDeleteConfirmationAlert = false
@State private var showDeletionFailedAlert = false
@State private var coursesToBeDeleted: [Course] = []
init(isPreview: Bool = false) {
self.isPreview = isPreview
self.vm = CourseListViewModel(manager: isPreview ? PersistenceManager.preview : PersistenceManager.shared)
}
var body: some View {
List {
ForEach(vm.allCourses) { course in
NavigationLink(destination: CourseEditView(isPreview: isPreview, course: course)) {
courseListItem(course)
}
}
.onDelete { indices in
coursesToBeDeleted = indices.map{ index in
vm.allCourses[index]
}
showDeleteConfirmationAlert.toggle()
}
}
.navigationTitle("Golf courses")
.toolbar {
ToolbarItem {
NavigationLink {
CourseEditView(isPreview: isPreview)
} label: {
Label("Add course", systemImage: "plus")
.labelStyle(.titleAndIcon)
}
}
}
.onAppear {
if refreshDataOnReappear {
vm.refresh()
refreshDataOnReappear = false
}
}
.onDisappear() {
refreshDataOnReappear = true
}
.alert("Delete Course", isPresented: $showDeleteConfirmationAlert) {
Button(role: .destructive) {
if !vm.deleteCourses(courses: coursesToBeDeleted) {
withAnimation {
showDeletionFailedAlert.toggle()
}
} else {
vm.refresh()
}
coursesToBeDeleted.removeAll()
} label: {
Text("Delete")
}
Button("Cancel", role: .cancel) { }
} message: {
Text("Deleting a course will delete all cards and stored rounds on this course! Are you sure you want to continue?")
}
.alert("Deletion Failed", isPresented: $showDeletionFailedAlert) {
Button("OK") {}
} message: {
Text("Attempt to delete the selected course failed. Restarting the app and trying again may resolve the issue.")
}
}
func courseListItem(_ course: Course) -> some View {
VStack(alignment: .leading, spacing: 4) {
Text(course.name)
if course.courseCards.count > 0 {
HStack(alignment: .top) {
Text("\(course.courseCards[0].cardHoles.count) holes ")
VStack(alignment: .leading, spacing: 4) {
ForEach(cardsByLength(course: course)) { card in
HStack {
Image(systemName: "menucard").foregroundColor(Color(uiColor: card.uiColor))
Text("\(card.teeColour): \(card.length) yards, par \(card.par)")
}
}
}
}
.padding(.leading)
.font(.caption)
} else {
Text("No cards entered yet!").font(.caption)
}
}
.padding(.vertical, 8)
}
func cardsByLength(course: Course) -> [Card] {
course.courseCards.sorted(by: { $0.length > $1.length })
}
}