Hi Everyone,
I'm coming to you hat-in-hand again, as I'm really struggling with a point of my app that keeps crashing when I go to click on a cell in my list. Namely I get this error: Thread 1: "-[AccountBackEnd accountCompanyName]: unrecognized selector sent to instance 0x6000025aac40.". This appears right on the line of @main in the file that renders the app's output. To make matters worse, this is in SwiftUI and there doesn't seem to be a lot of documentation on this error online.
I'm not 100% sure, but I think it's got something to do with a syste for navigation that I downloaded, I was using which was meant to make navigation simpler. I don't think that's the case here. I've begun the process of going away from that system of buttons to use the more conventional NavigationView and NavigationLink.
I've managed to deal with a few errors already here, but this one seems to be kicking my **** pretty badly. I'm at a loss. I'll put my code below for your review.
As always, any help here is greatly, greatly appreciated.
Database view, this contains the app's list
struct DatabaseViewMain: View {
//Fetch request for pulling from persistent store.
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \AccountBackEnd.accountCompanyName, ascending: true)],
animation: .default)
private var accounts: FetchedResults<AccountBackEnd>
//Calls to Class for published objects
@EnvironmentObject var publishedClasses: PublishedClasses
//Takes object from @StateObject, presents as var
@ObservedObject var accountBackEnd: AccountBackEnd = AccountBackEnd()
var body: some View {
VStack {
HStack {
Spacer()
NavigationLink {
AddAccountView()
} label: {
SubHeaderViewIcon(subheaderIcon: "plus.square", subheaderIconColor: Color("EditButtonBlue"))
}
.padding()
}
List {
ForEach(accounts, id: \.self) {account in
NavigationLink {
AccountDetailMain(accountBackEnd: accountBackEnd)
} label: {
Text(account.accountCompanyName ?? "")
}
}
.onDelete(perform: deleteAccounts)
.environmentObject(publishedClasses)
}
.listStyle(PlainListStyle())
}
.navigationBarHidden(true)
.environmentObject(publishedClasses)
}
//Deleting Account
private func deleteAccounts(offsets: IndexSet) {
withAnimation {
offsets.map { accounts[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
print("Shit. Something happened. Error \(error.localizedDescription)")
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
}
AccountDetailMain, this presents the user with their Account's info. I believe this is the part that is actually crashing the app?
struct AccountDetailMain: View {
//Managed Object Context Call
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \AccountBackEnd.accountID, ascending: true)],
animation: .default)
private var accounts: FetchedResults<AccountBackEnd>
//Calls from @Published
@EnvironmentObject var publishedClasses: PublishedClasses
//Takes object from @StateObject, presents as var
@ObservedObject var accountBackEnd: AccountBackEnd
var body: some View {
ScrollView {
VStack {
MainHeaderViewTop(mainTextField: accountBackEnd.accountCompanyName ?? "", iconField: "building")
}
.environmentObject(publishedClasses)
Spacer()
.frame(height: 5)
HStack {
NavigationLink {
EditAccountView()
} label: {
MainHeaderViewBottomLeft()
}
Spacer()
NavigationLink {
AddAccountView()
} label: {
SubHeaderViewIcon(subheaderIcon: "plus.square", subheaderIconColor: Color("EditButtonBlue"))
}
}
ViewSpacer()
Group {
SalesRecords()
ViewSpacer()
LatestThreeQuotes()
ViewSpacer()
LatestNote()
ViewSpacer()
}
Group {
AccountsContactsView()
ViewSpacer()
BranchContactInfoView()
ViewSpacer()
AccountAddressView()
ViewSpacer()
RepDetailsView()
ViewSpacer()
}
Group {
NavigationLink {
EditAccountView()
} label: {
LargeEditButtonGreen(editButtonLabel: "Edit Account")
}
ViewSpacer()
}
.environmentObject(publishedClasses)
}
.navigationBarHidden(true)
.environmentObject(publishedClasses)
}
}