TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Core Data: Failed to call designated initializer on NSManagedObject

Forums > SwiftUI

I have created Book Entity using core data. As users click on the BookCardView, it should know what book is clicked on. My approach is create UserCardViewModel, which keep track what book was selected

class UserCardViewModel: ObservableObject {
  @Published var selectedCard : Book = Book()
}

In UserView

struct UserView: View {
  @StateObject var userCardModel = UserCardViewModel()

  var body: some View {
    ForEach(books){book in
      BookCardView(selectedCard: $userCardModel.selectedCard)
    }
}

The code was built successfully but it show errors on debug like Failed to call designated initializer on NSManagedObject class 'Book' I guess the problem is in UserCardViewModel as I try to initialize instance of Book without given context. Any ideas how to fix this problem

3      

if Book using Core Data, I think the type should be FetchResults<Book>.Element . but I init like this:

class Current: ObservableObject {
    @Published var currentArticle = FetchedResults<Article>.Element()
}

still wrong ...

3      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.