UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.