I get this Issue : ContentView.swift:3016:25 The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions. But I don´t know what I have to do. I am a noob. Can anybody help me.

// Füge OrderRow hinzu struct OrderRow: View { let order: Order

var body: some View {
    VStack(alignment: .leading, spacing: 8) {
        HStack {
            Text("Tisch \(order.tableNumber)")
                .font(.headline)
            
            Spacer()
            
            Text(order.status.rawValue)
                .font(.caption)
                .padding(.horizontal, 8)
                .padding(.vertical, 4)
                .background(order.status.color.opacity(0.2))
                .foregroundColor(order.status.color)
                .cornerRadius(8)
        }
        
        Text(order.timestamp, style: .time)
            .font(.caption)
            .foregroundColor(.gray)
        
        ForEach(order.items) { item in
            Text("\(item.quantity)x \(item.item.name)")
                .font(.subheadline)
        }
        
        HStack {
            Spacer()
            Text(String(format: "%.2f €", order.totalAmount))
                .bold()
        }
    }
    .padding()
    .background(Color.white)
    .cornerRadius(12)
    .shadow(radius: 2)
}

} struct MenuItemRow: View { let item: MenuItem

    var body: some View {
        HStack {
            Text(item.name)
                .font(.headline)
            Spacer()
            Text(String(format: "%.2f €", item.price))
                .font(.subheadline)
        }
        .padding()
    }
}

struct MenuItemsView: View { @Binding var selectedItems: [OrderItem] @Environment(.dismiss) private var dismiss

func filteredItems(for category: MenuCategory) -> [MenuItem] {
    return sampleMenuItems.filter { $0.category == category }
}

func addItemToOrder(_ item: MenuItem) {
    if let index = selectedItems.firstIndex(where: { $0.item.id == item.id }) {
        selectedItems[index].quantity += 1
    } else {
        selectedItems.append(OrderItem(item: item, quantity: 1))
    }
}
var body: some View {
    NavigationView {
        List {
            ForEach(MenuCategory) { category in
                Section(header: Text(category.rawValue)) {
                    let filteredItems = sampleMenuItems.filter
                    
                    ForEach(filteredItems) { item in

                    }
                        MenuItemRow(item: sampleMenuItems.filter) {
                            addItemToOrder
                        }
                    }
                }
            }
        }
        .navigationTitle("Menü")
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                Button("Fertig") {
                    dismiss()
                }
            }
        }
    }
}

}

// Sample Menü-Items let sampleMenuItems: [MenuItem] = [ MenuItem(name: "Cola", description: "0,33l", price: 3.50, category: .drinks, image: "cola"), MenuItem(name: "Sprite", description: "0,33l", price: 3.50, category: .drinks, image: "sprite"), MenuItem(name: "Bier", description: "0,5l", price: 4.00, category: .drinks, image: "beer"), MenuItem(name: "Nachos", description: "mit Käsesauce", price: 5.50, category: .snacks, image: "nachos"), MenuItem(name: "Pommes", description: "mit Ketchup", price: 4.50, category: .food, image: "fries"), MenuItem(name: "********", description: "alkoholfrei", price: 6.50, category: .specials, image: "********") ]

struct NewEventView: View { var body: some View { Text("New Event") } }

struct EventManagementView: View { var body: some View { Text("Event Management") } }

struct OrderRow: View { var order: String var body: some View { Text("Order: (order)") } }

#Preview { ContentView() }

Answered by darkpaw in 815576022

You have a few lines where you do something like sampleMenuItems.filter but you haven't told it what to filter for.

You need to do something like this: let filteredItems = sampleMenuItems.filter { $0.cost > 100 }

And, as the error is telling you, you need to split your code into smaller chunks to see where the error is. Just comment out a section at a time and see whether the error disappears. If it does, then the error is in that bit you commented.

Welcome to the forum.

But your post is really badly written:

  • When you post code, use code formatter to make your code readable.
  • avoid duplicating the post
  • provide all structure definitions, otherwise impossible to test and investigate.
  • make sure the code you post does compile:

What are:

  • OrderItem
  • MenuCategory
  • etc

Your code compile with those error. What di you post ? Real code ? An edited version (bad idea):

    @Environment(.dismiss) private var dismiss

It should be

    @Environment(\.dismiss) private var dismiss

This as well if MenuCategory is a type definition and not a var

                ForEach(MenuCategory) { category in
struct OrderRow: View

is declared twice

Accepted Answer

You have a few lines where you do something like sampleMenuItems.filter but you haven't told it what to filter for.

You need to do something like this: let filteredItems = sampleMenuItems.filter { $0.cost > 100 }

And, as the error is telling you, you need to split your code into smaller chunks to see where the error is. Just comment out a section at a time and see whether the error disappears. If it does, then the error is in that bit you commented.

Can you give me the right code to solve this issue please

struct MenuItemsView: View {
    @Binding var selectedItems: [OrderItem]
    @Environment(\.dismiss) private var dismiss
    
    func filteredItems(for category: MenuCategory) -> [MenuItem] {
        return sampleMenuItems.filter { $0.category == category }
    }
    
    func addItemToOrder(_ item: MenuItem) {
        if let index = selectedItems.firstIndex(where: { $0.item.id == item.id }) {
            selectedItems[index].quantity += 1
        } else {
            selectedItems.append(OrderItem(item: item, quantity: 1))
        }
    }
    var body: some View {
        NavigationView {
            List {
                ForEach(MenuCategory) { category in
                    Section(header: Text(category.rawValue)) {
                        let filteredItems = sampleMenuItems.filter
                        
                        ForEach(filteredItems) { item in

                        }
                            MenuItemRow(item: sampleMenuItems.filter) {
                                addItemToOrder
                            }
                        }
                    }
                }
            }
            .navigationTitle("Menü")
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("Fertig") {
                        dismiss()
                    }
                }
            }
        }
    }
}


// Sample Menü-Items
let sampleMenuItems: [MenuItem] = [
    MenuItem(name: "Cola", description: "0,33l", price: 3.50, category: .drinks, image: "cola"),
    MenuItem(name: "Sprite", description: "0,33l", price: 3.50, category: .drinks, image: "sprite"),
    MenuItem(name: "Bier", description: "0,5l", price: 4.00, category: .drinks, image: "beer"),
    MenuItem(name: "Nachos", description: "mit Käsesauce", price: 5.50, category: .snacks, image: "nachos"),
    MenuItem(name: "Pommes", description: "mit Ketchup", price: 4.50, category: .food, image: "fries"),
    MenuItem(name: "********", description: "alkoholfrei", price: 6.50, category: .specials, image: "********")
]

struct NewEventView: View {
    var body: some View {
        Text("New Event")
    }
}

struct EventManagementView: View {
    var body: some View {
        Text("Event Management")
    }
}

struct OrderRow: View {
    var order: String
    var body: some View {
        Text("Order: \(order)")
    }
}

#Preview {
    ContentView()
}

You just need to apply the right filters. You have this function:

func filteredItems(for category: MenuCategory) -> [MenuItem] {
    return sampleMenuItems.filter { $0.category == category }
}

It filters the sampleMenuItems and returns only those items that match the category.

This line: let filteredItems = sampleMenuItems.filter in your ForEach... is pointless and is likely causing your issue.

This:

ForEach(filteredItems) { item in

}

has nothing in it. Also, it's a code smell. You've got a variable called filteredItems AND a function with the same name. Don't do this.

This:

MenuItemRow(item: sampleMenuItems.filter) {
    addItemToOrder
}

Again, the sampleMenuItems.filter bit is incorrect.

I get this Issue : ContentView.swift:3016:25 The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions. But I don´t know what I have to do. I am a noob. Can anybody help me.
 
 
Q