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

Day 59, Project 12, Challenge 2

Forums > 100 Days of SwiftUI

Hi im getting errors in ContentView in ForEach lines. What do you think is wrong here?

import SwiftData
import SwiftUI

struct ContentView: View {

    @State private var items: ExpenseItem

    @Environment(\.modelContext) var modelContext

    @State private var sortOrder = [

        SortDescriptor(\ExpenseItem.name),
        SortDescriptor(\ExpenseItem.amount)

    ]

    @State private var showingAddExpense = false

    var body: some View {
        NavigationView {
            List {
                Section(header: Text("Personal")) {
                    ForEach(items.filter { $0.type == "Personal" }) { item in
                        ExpenseRow(item: item)
                    }
                }

                Section(header: Text("Business")) {
                    ForEach(items.filter { $0.type == "Business" }) { item in
                        ExpenseRow(item: item)
                    }
                }
            }
            .navigationTitle("iExpense")
            .toolbar {
                Button {
                    showingAddExpense.toggle()
                } label: {
                    Image(systemName: "plus")
                }
                Menu("Sort", systemImage: "arrow.up.arrow.down") {
                    Picker("Sort", selection: $sortOrder) {
                        Text("Dort by name")
                            .tag([
                                SortDescriptor(\ExpenseItem.name),
                                SortDescriptor(\ExpenseItem.amount)
                            ])

                        Text("Dort by Amount")
                            .tag([
                                SortDescriptor(\ExpenseItem.amount),
                                SortDescriptor(\ExpenseItem.name)
                            ])
                    }
                }
                }
            }
            .sheet(isPresented: $showingAddExpense) {
                AddView()
            }

        }
    }

struct ExpenseRow: View {
    var item: ExpenseItem

    var body: some View {
        HStack {
            VStack(alignment: .leading) {
                Text(item.name)
                    .font(.headline)
                Text(item.type)
            }

            Spacer()

            Text(item.amount, format: .currency(code: "PLN"))
                .customStyle(item.amount)
        }
    }
}

extension Text {
    func customStyle(_ amount: Double) -> Text {
        switch amount {
        case ..<10.0:
            return foregroundStyle(.green)
        case ..<100.0:
            return foregroundStyle(.blue)
        default:
            return foregroundStyle(.red)
        }
    }
}

2      

Can anyone give me some tips here? :)

2      

Firstly, item @State private var items: ExpenseItem declares a single ExpenseItem instead of a list of expense items so using ForEach with a single ExpenseItem won't work here, you'll want to declare an array instead. @State private var items: [ExpenseItem]

Secondly, as you are now rebuilding this project with SwiftData, instead, you should query your model object using @Query. So something like @Query var items: [ExpenseItem]

Hope this helps!

2      

In the ContentView. It's declared as a single ExpenseItem instead of an array of ExpenseItem. The ForEach expects a collection, but you are providing a single instance of ExpenseItem. so fix that using

@State private var items : [ExpenseItem]

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.