So I was just following along with CodeWithChris's video on make a persistent data storage (https://youtu.be/O7u9nYWjvKk) and I got to the part where he says to include the subclass NSManagedObject to the family and person classes. I have a very similar program with recipe and list of recipes, but when I add "NSManagedObject" I get an error:
< unknown >:0: error: stored property '_title' requires an initial value CoreData.NSManagedObject:2:12: note: superclass 'NSManagedObject' requires all stored properties to have initial values open class NSManagedObject : NSObject {
< unknown > :0: error: stored property '_calories' requires an initial value CoreData.NSManagedObject:2:12: note: superclass 'NSManagedObject' requires all stored properties to have initial values open class NSManagedObject : NSObject {
etc.
Unsure of how to fix it. Any help would be greatly appreciated.
Code in case I'm missing something
RecipeList:
import Foundation
import CoreData
class RecipeList:NSManagedObject, Identifiable{
@Published var recipeList:[Recipe]
init(){
recipeList=[]
}
func addRecipe(recipe: Recipe){
self.recipeList.append(recipe)
}
func removeRecipe(loc: Int){
self.recipeList.remove(at: loc)
}
func getList() -> Array<Recipe>{
return recipeList
}
}
Recipe:
import Foundation
import UIKit
import CoreData
class Recipe:NSManagedObject, Identifiable{
@Published var title: String
@Published var calories: String
@Published var ingredients: Array<String>
@Published var instructions: Array<String>
@Published var totalTime: String
@Published var notes: String
init(title: String, calories: String, ingredients: Array<String>, instructions: Array<String>, totalTime: String, notes: String){
self.title=title
self.calories=calories
self.ingredients=ingredients
self.instructions=instructions
self.totalTime=totalTime
self.notes=notes
}
}