GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.