TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.