How do I fix ‘unrecognized selector sent to instance’ in swift?

I am working on an app using coreData and a log in. For that, I have a LoginView and LoginViewModel that I use to get the info to the ContentView. I keep getting this error message: “Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[abcText2Speech.UserEntity firstname]: unrecognized selector sent to instance 0x6000017f14c0′”

With this callstack:
*** First throw call stack: ( 0 CoreFoundation 0x00007ff80049b761 __exceptionPreprocess + 242 1 libobjc.A.dylib 0x00007ff800063904 objc_exception_throw + 48 2 CoreFoundation 0x00007ff8004b0873 +[NSObject(NSObject) instanceMethodSignatureForSelector:] + 0 3 CoreFoundation 0x00007ff80049fef4 ___forwarding___ + 1459 4 CoreFoundation 0x00007ff8004a20f8 _CF_forwarding_prep_0 + 120 5 abcText2Speech 0x000000010b5dd5ce $s14abcText2Speech11ContentViewV4bodyQrvg7SwiftUI05TupleE0VyAE6SpacerV_AE4TextVAE0E0PAEE5frame5width6height9alignmentQr12CoreGraphics7CGFloatVSg_AuE9AlignmentVtFQOyAmEE15foregroundColoryQrAE0U0VS 6 abcText2Speech 0x000000010b5e867d $s14abcText2Speech11ContentViewV4bodyQrvg7SwiftUI05TupleE0VyAE6SpacerV_AE4TextVAE0E0PAEE5frame5width6height9alignmentQr12CoreGraphics7CGFloatVSg_AuE9AlignmentVtFQOyAmEE15foregroundColoryQrAE0U0VS 7 SwiftUI 0x00007ff84a238bd1 block_destroy_helper.34 + 287 8 abcText2Speech 0x000000010b5dced7 $s14abcText2Speech11ContentViewV4bodyQrvg + 775 9 abcText2Speech 0x000000010b5e6fb7 $s14abcText2Speech11ContentViewV7SwiftUI0E0AadEP4body4BodyQzvgTW + 55 10 SwiftUI 0x00007ff84a400092 __swift_destroy_boxed_opaque_existential_0Tm + 1400748 11 SwiftUI 0x00007ff84b293585 objectdestroy.15Tm + 35474 12 SwiftUI 0x00007ff84b2932ba objectdestroy.15Tm + 34759 13 SwiftUI 0x00007ff84ad275d0 __swift_allocate_boxed_opaque_existential_1Tm + 209 14 AttributeGraph 0x00007ff838cc1e8b _ZN2AG5Graph11UpdateStack6updateEv + 601 15 AttributeGraph 0x00007ff838cc266e _ZN2AG5Graph16update_attributeENS_4data3ptrINS_4NodeEEEj + 446 16 AttributeGraph 0x00007ff838cca39a _ZN2AG5Graph20input_value_ref_slowENS_4data3ptrINS_4NodeEEENS_11AttributeIDEjjPK15AGSwiftMetadataRhl + 638 17 AttributeGraph 0x00007ff838ce28a6 AGGraphGetValue + 233 18 SwiftUI 0x00007ff84b293244 objectdestroy.15Tm + 34641 19 SwiftUI 0x00007ff84ad275d0 __swift_allocate_boxed_opaque_existential_1Tm + 209 20 AttributeGraph 0x00007ff838cc1e8b _ZN2AG5Graph11UpdateStack6updateEv + 601 21 AttributeGraph 0x00007ff838cc266e _ZN2AG5Graph16update_attributeENS_4data3ptrINS_4NodeEEEj + 446 22 AttributeGraph 0x00007ff838cca39a _ZN2AG5Graph20input_value_ref_slowENS_4data3ptrINS_4NodeEEENS_11AttributeIDEjjPK15AGSwiftMetadataRhl + 638 23 AttributeGraph 0x00007ff838ce28a6 AGGraphGetValue + 233 24 SwiftUI 0x00007ff84aae3cb9 __swift_allocate_boxed_opaque_existential_1Tm + 547472 25 AttributeGraph 0x00007ff838cc1e8b _ZN2AG5Graph11UpdateStack6updateEv + 601 26 AttributeGraph 0x00007ff838cc266e _ZN2AG5Graph16update_attributeENS_4data3ptrINS_4NodeEEEj + 446 27 AttributeGraph 0x00007ff838cca39a _ZN2AG5Graph20input_value_ref_slowENS_4data3ptrINS_4NodeEEENS_11AttributeIDEjjPK15AGSwiftMetadataRhl + 638 28 AttributeGraph 0x00007ff838ce28a6 AGGraphGetValue + 233 29 SwiftUI 0x00007ff84a05a9a4 keypath_getTm + 3166475 30 SwiftUI 0x00007ff84a0e408a objectdestroy.843Tm + 27632 31 AttributeGraph 0x00007ff838cc1e8b _ZN2AG5Graph11UpdateStack6updateEv + 601 32 AttributeGraph 0x00007ff838cc266e _ZN2AG5Graph16update_attributeENS_4data3ptrINS_4NodeEEEj + 446 33 AttributeGraph 0x00007ff838cca39a _ZN2AG5Graph20input_value_ref_slowENS_4data3ptrINS_4NodeEEENS_11AttributeIDEjjPK15AGSwiftMetadataRhl + 638 34 AttributeGraph 0x00007ff838ce28a6 AGGraphGetValue + 233 35 SwiftUI 0x00007ff84aae3bdb __swift_allocate_boxed_opaque_existential_1Tm + 547250 36 AttributeGraph 0x00007ff838cc1e8b _ZN2AG5Graph11UpdateStack6updateEv + 601 37 AttributeGraph 0x00007ff838cc266e _ZN2AG5Graph16update_attributeENS_4data3ptrINS_4NodeEEEj + 446 38 AttributeGraph 0x00007ff838cca39a _ZN2AG5Graph20input_value_ref_slowENS_4data3ptrINS_4NodeEEENS_11AttributeIDEjjPK15AGSwiftMetadataRhl + 638 39 AttributeGraph 0x00007ff838ce28a6 AGGraphGetValue + 233 40 SwiftUI 0x00007ff84b292ad7 objectdestroy.15Tm + 32740 41 SwiftUI 0x00007ff84b292e2d objectdestroy.15Tm + 33594 42 SwiftUI 0x00007ff84b292bfe objectdestroy.15Tm + 33035 43 SwiftUI 0x00007ff84ad275d0 __swift_allocate_boxed_opaque_existential_1Tm + 209 44 AttributeGraph 0x00007ff838cc1e8b _ZN2AG5Graph11UpdateStack6updateEv + 601 45 AttributeGraph 0x00007ff838cc266e _ZN2AG5Graph16update_attributeENS_4data3ptrINS_4NodeEEEj + 446 46 AttributeGraph 0x00007ff838cca39a _ZN2AG5Graph20input_value_ref_slowENS_4data3ptrINS_4NodeEEENS_11AttributeIDEjjPK15AGSwiftMetadataRhl + 638 47 AttributeGraph 0x00007ff838ce28a6 AGGraphGetValue + 233 48 SwiftUI 0x00007ff84b292ad7 objectdestroy.15Tm + 32740 49 SwiftUI 0x00007ff84b292e2d objectdestroy.15Tm + 33594 50 SwiftUI 0x00007ff84b292bfe objectdestroy.15Tm + 33035 51 SwiftUI 0x00007ff84ad275d0 __swift_allocate_boxed_opaque_existential_1Tm + 209 52 AttributeGraph 0x00007ff838cc1e8b _ZN2AG5Graph11UpdateStack6updateEv + 601 53 AttributeGraph 0x00007ff838cc266e _ZN2AG5Graph16update_attributeENS_4data3ptrINS_4NodeEEEj + 446 54 AttributeGraph 0x00007ff838cca39a _ZN2AG5Graph20input_value_ref_slowENS_4data3ptrINS_4NodeEEENS_11AttributeIDEjjPK15AGSwiftMetadataRhl + 638 55 AttributeGraph 0x00007ff838ce28a6 AGGraphGetValue + 233 56 SwiftUI 0x00007ff84b292ad7 objectdestroy.15Tm + 32740 57 SwiftUI 0x00007ff84b292e2d objectdestroy.15Tm + 33594 58 SwiftUI 0x00007ff84b292bfe objectdestroy.15Tm + 33035 59 SwiftUI 0x00007ff84ad275d0 __swift_allocate_boxed_opaque_existential_1Tm + 209 60 AttributeGraph 0x00007ff838cc1e8b _ZN2AG5Graph11UpdateStack6updateEv + 601 61 AttributeGraph 0x00007ff838cc266e _ZN2AG5Graph16update_attributeENS_4data3ptrINS_4NodeEEEj + 446 62 AttributeGraph 0x00007ff838cca39a _ZN2AG5Graph20input_value_ref_slowENS_4data3ptrINS_4NodeEEENS_11AttributeIDEjjPK15AGSwiftMetadataRhl + 638 63 AttributeGraph 0x00007ff838ce28a6 AGGraphGetValue + 233 64 SwiftUI 0x00007ff84b292ad7 objectdestroy.15Tm + 32740 65 SwiftUI 0x00007ff84b292e2d objectdestroy.15Tm + 33594 66 SwiftUI 0x00007ff84b292bfe objectdestroy.15Tm + 33035 67 SwiftUI 0x00007ff84ad275d0 __swift_allocate_boxed_opaque_existential_1Tm + 209 68 AttributeGraph 0x00007ff838cc1e8b _ZN2AG5Graph11UpdateStack6updateEv + 601 69 AttributeGraph 0x00007ff838cc266e _ZN2AG5Graph16update_attributeENS_4data3ptrINS_4NodeEEEj + 446 70 AttributeGraph 0x00007ff838cca39a _ZN2AG5Graph20input_value_ref_slowENS_4data3ptrINS_4NodeEEENS_11AttributeIDEjjPK15AGSwiftMetadataRhl + 638 71 A"

Here is my code where I think the issue lies :

this is from LoginViewModel:

` func signup()-> (UserEntity, Int){
@StateObject var dataController = DataController.shared

    buttonState = .loading(title: "Loading", systemImage: "person")
    
    DispatchQueue.main.asyncAfter(deadline: .now()+1.5){
        if self.isValidEmail() && self.isValidPassword() && self.isValidName(){
            self.isLoggedIn = true
            dataController.addUser(firstname: self.fname, lastname: self.lname, email: self.email, password: self.password)
            
            print("signed in")
            print(dataController.savedEntities[(dataController.savedEntities.count)-1].email ?? "email")
            print(self.isLoggedIn)
            self.buttonState = .enabled(title: "Login", systemImage: "checkmark.circle")
        } else{
            self.buttonState = .disabled(title: "Fill out all fields to log in", systemImage: "exclamationmark.circle")
        }
    }
    print("Test")
    print(dataController.savedEntities[(dataController.savedEntities.count)-1].email ?? "email")
        
    return (dataController.savedEntities[(dataController.savedEntities.count)-1], 1)

}`

and this all prints with no issue.

this is the relevant part of sign up view

` ActionButton(state: $model.buttonState, onTap:{
signup = model.signup()
user = signup.0

                    }, backgroundColor: .primary , foregroundColor: Color(UIColor.systemBackground)
                    )
                    NavigationLink (destination: LoginView()){
                        Text("Already have a profile? Click here to Log in")
                    }
                }
            } label: {
                Label("Welcome!", systemImage: "hand.wave")
            }
            .padding()
            .textFieldStyle(.plain)
            let keyboard = abcTextViewModel()
            
            NavigationLink (destination: ContentView(viewModel: keyboard, currentUser: user) , isActive: $model.isLoggedIn){
                
                EmptyView()
            }`

and this is the part of Content view that breaks it

Text(currentUser.firstname ?? "name")
these are the details of UserEntity

and this is the DataController code

`
import Foundation
import CoreData

class DataController: ObservableObject{
static let shared = DataController()

let container: NSPersistentContainer
@Published var savedEntities: [UserEntity] = []

init(){
    container = NSPersistentContainer(name: "Model")
    container.loadPersistentStores { description, error in
        if let error = error {
            print("Core Data failed to load: (error.localizedDescription)")
        }
    }
    fetchUsers()
}

func fetchUsers() {
    let request = NSFetchRequest<UserEntity>(entityName: "UserEntity")
    
    do {
        savedEntities = try container.viewContext.fetch(request)
    } catch let error {
        print("Error while fetching: (error)")
    }
}

func addUser(firstname: String, lastname: String, email: String, password: String) {
    let newUser = UserEntity(context: container.viewContext)
    newUser.firstname = firstname
    newUser.lastname = lastname
    newUser.email = email
    newUser.id = password
    saveData()
}

func saveData(){
    do {
        try container.viewContext.save()
        fetchUsers()
    } catch let error {
        print("Error saving: (error)")
    }
} }`

any help would be greatly appreciated, I have been stuck on this one error for quite a while

I tried printing the user details in login view model which worked, however that did not carry over to content view.

New contributor

Jaanvi chirimar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật