I had the idea of adding a list feature in my app where users can create a list and add multiple recipes to the app so first and only thing I did was add an entity to my data model called "List" that contains a string: title and an array of Recipes: recipes (I remembered to put "NSSecureUnarchiveFromData" in Transformer and put [Recipe] for custom class). Afterwards I made the relationship in both entities and made them inverse.
I made no other changes to my code. But I ran it just to make sure nothing went wrong and lo and behold: 7 never before seen errors, but only in one file. Before adding this entity this same code compiled just fine. This is the file and these are the errors I'm getting. Any help would be greatly appreciated.
import SwiftUI
struct RecipeView: View {
@Environment (\.managedObjectContext) var managedObjContext
@Environment(\.dismiss) var dismiss
var recipe: FetchedResults<Recipe>.Element
@State var isFavorite: Bool
@State var servings = -1
var body: some View {
VStack(alignment: .leading){ //Error: Trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure
if (recipe.notes! != ""){
Section{
Text(recipe.notes!)
.font(.headline)
}
.padding(.horizontal)
}
HStack{
Spacer()
Text("Total Time: "+calcTime(time:Int(recipe.totalTime!) ?? 0))
Spacer()
Text("Servings: "+recipe.servings!)
Spacer()
}
.padding(.vertical)
Grid{
GridRow{
Button {
isFavorite.toggle()
recipe.isFavorite.toggle()
PersistenceController().save(context: managedObjContext)
} label: {
HStack{
Image(systemName: isFavorite ? "star.fill" : "star")
.foregroundStyle(.yellow)
Text(isFavorite ? "Unfavorite" : "Favorite")
.foregroundColor(Color(UIColor.lightGray))
}
.frame(width: 300,height: 50)
.background(Color(UIColor(hexString: "#202020")))
.border(Color(UIColor(hexString: "#202020")))
.cornerRadius(5)
}
Button {
print("implement list functionality")
} label: {
Image(systemName: "plus")
.frame(width: 50,height: 50)
.background(Color(UIColor(hexString: "#202020")))
.border(Color(UIColor(hexString: "#202020")))
.cornerRadius(5)
}
}
}
.padding(.horizontal)
List{
NavigationLink(destination: ingredientsView(ingredients: recipe.ingredients!)){
HStack{
Text("List of Ingredients")
Spacer()
Text(String(recipe.ingredients!.count))
.foregroundColor(.gray)
}
}
.frame(height: 50)
NavigationLink(destination: instructionsView(instructions: recipe.instructions!)){
HStack{
Text("List of Instructions")
Spacer()
Text(String(recipe.instructions!.count))
.foregroundColor(.gray)
}
}
.frame(height: 50)
}
.listStyle(.grouped)
.scrollDisabled(true)
Spacer()
}
.navigationBarTitle(recipe.title!)
.navigationBarItems(trailing: shareButton)
.onAppear{
PersistenceController().updateDate(recipe: recipe, context: managedObjContext)
}
Spacer()
}
var shareButton: some View{
Button(action: {
print("Implement airdrop feature")
}){
Image(systemName: "square.and.arrow.up")
.foregroundStyle(.blue)
}
}
}
struct ingredientsView: View{
@State var ingredients: [String]
var body: some View{
List{ // Error: Trailing closure passed to parameter of type 'NSManagedObjectContext' that does not accept a closure
Section(""){
ForEach(ingredients,id: \.self){ String in
NavigationLink(destination:
NavigationView{
Text(String)
.frame(alignment:.center)
.font(.title)
}){
Text(String).lineLimit(1)
}
}
}
}
.frame(alignment: .center) //Error: Cannot infer contextual base in reference to member 'center'
//Error: Value of type 'List' has no member 'frame'
.cornerRadius(10)
.navigationTitle("Ingredients List")
}
}
struct instructionsView: View{
@State var instructions: [String]
var body: some View{
List{ // Error: Trailing closure passed to parameter of type 'NSManagedObjectContext' that does not accept a closure
Section(""){
ForEach(instructions,id: \.self){ String in
NavigationLink(destination:
NavigationView{
Text(String)
.frame(alignment:.center)
.font(.title)
}){
Text(String).lineLimit(1)
}
}
}
}
.frame(alignment: .center) //Error: Cannot infer contextual base in reference to member 'center'
//Error: Value of type 'List' has no member 'frame'
.cornerRadius(10)
.navigationTitle("Instructions List")
}
}
-
—
WeagleWeagle
Add a CommentI think it has something to do with the relationship part because just making a plain entity in the data model isn't a problem. Is there something more you have to do for the relationship?