SwiftUI: I keep getting this error: "Thread 1: "-[AccountBackEnd accountCompanyName]: unrecognized selector sent to instance 0x6000025aac40""

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)
    }
   
}

Can you show the class definition of AccountBackEnd? And the entity definition of AccountBackEnd in your Core Data model?

Of course - please see my second post.

Of Course:

import Foundation
import CoreData

@objc(AccountBackEnd)
public class AccountBackEnd: NSManagedObject {
 
}

And...

import Foundation

import CoreData





extension AccountBackEnd {



    @nonobjc public class func fetchRequest() -> NSFetchRequest<AccountBackEnd> {

        return NSFetchRequest<AccountBackEnd>(entityName: "AccountBackEnd")

    }



    @NSManaged public var accountAddress: String?

    @NSManaged public var accountCity: String?

    @NSManaged public var accountCompanyName: String?

    @NSManaged public var accountCustomerType: String?

    @NSManaged public var accountEmailAddress: String?

    @NSManaged public var accountID: UUID?

    @NSManaged public var accountLastDateVisited: String?

    @NSManaged public var accountMarketType: String?

    @NSManaged public var accountPhoneOne: String?

    @NSManaged public var accountPhoneTwo: String?

    @NSManaged public var accountPostalCode: String?

    @NSManaged public var accountProvince: String?

    @NSManaged public var accountManager: String?

    @NSManaged public var accountWebsite: String?

    @NSManaged public var toContacts: NSSet?

    @NSManaged public var toQuotes: NSSet?

    @NSManaged public var toNotes: NSSet?



}



// MARK: Generated accessors for toContacts

extension AccountBackEnd {



    @objc(addToContactsObject:)

    @NSManaged public func addToToContacts(_ value: ContactBackEnd)



    @objc(removeToContactsObject:)

    @NSManaged public func removeFromToContacts(_ value: ContactBackEnd)



    @objc(addToContacts:)

    @NSManaged public func addToToContacts(_ values: NSSet)



    @objc(removeToContacts:)

    @NSManaged public func removeFromToContacts(_ values: NSSet)



}



// MARK: Generated accessors for toQuotes

extension AccountBackEnd {



    @objc(addToQuotesObject:)

    @NSManaged public func addToToQuotes(_ value: QuotesBackEnd)



    @objc(removeToQuotesObject:)

    @NSManaged public func removeFromToQuotes(_ value: QuotesBackEnd)



    @objc(addToQuotes:)

    @NSManaged public func addToToQuotes(_ values: NSSet)



    @objc(removeToQuotes:)

    @NSManaged public func removeFromToQuotes(_ values: NSSet)



}



// MARK: Generated accessors for toNotes

extension AccountBackEnd {



    @objc(addToNotesObject:)

    @NSManaged public func addToToNotes(_ value: NoteBackEnd)



    @objc(removeToNotesObject:)

    @NSManaged public func removeFromToNotes(_ value: NoteBackEnd)



    @objc(addToNotes:)

    @NSManaged public func addToToNotes(_ values: NSSet)



    @objc(removeToNotes:)

    @NSManaged public func removeFromToNotes(_ values: NSSet)



}



extension AccountBackEnd : Identifiable {



}

Thanks for showing the code. But how is the entity definition in your data model?

The error you have described often happens when the class definition and the entity definition are inconsistent.

I see what you mean, but from what I can tell, this its the same as what I'm calling. Pics attached.

As well, here is the section that I think is having the issue.

Thanks for showing the entity definition. I can eliminate one possibility with it.

Another possibility causing this issue might reside in this line:

@ObservedObject var accountBackEnd: AccountBackEnd = AccountBackEnd()

With instantiating a Core Data entity class with .init() like AccountBackEnd(), the instance has nothing to do with the entity definition, so any sort of access to @NSManaged properties will cause the error.

Isn't this line in DatabaseViewMain

                        AccountDetailMain(accountBackEnd: accountBackEnd)

should be something like this?

                        AccountDetailMain(accountBackEnd: account)

You won't believe this. So its 5am where I'm at and I can't sleep. I wake up to avoid keeping my partner awake, I check this, and OH MY GODD!! It works !!!!

Thank you!!!

I'm always amazed at the kindness of Internet strangers.

Cheers! And thank you again.

SwiftUI: I keep getting this error: "Thread 1: "-[AccountBackEnd accountCompanyName]: unrecognized selector sent to instance 0x6000025aac40""
 
 
Q