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

iExpense, Day 37, Cannot find 'Expenses' in scope

Forums > SwiftUI

@YKN  

Hello everyone, I have a problem with day 37 "Sharing an observed object with a new view". I get the error message: "Cannot find 'Expenses' in scope". Even after several tries and new codes, it didn't work. Can anyone help me?

import SwiftUI

struct ContentView: View {
    struct ExpenseItem: Identifiable {
        let id = UUID()
        let name: String
        let type: String
        let amount: Double
    }

    class Expenses: ObservableObject {
        @Published var items = [ExpenseItem]()
    }

    @StateObject var expenses = Expenses()

    func removeItems(at offsets: IndexSet) {
        expenses.items.remove(atOffsets: offsets)
    }

    @State private var showingAddExpense = false

    var body: some View {
        NavigationView {
            List {
                ForEach(expenses.items) { item in
                    Text(item.name)
                }
                .onDelete(perform: removeItems)

            }
            .navigationTitle("iExpense")

            .toolbar {
                Button {
                    showingAddExpense = true
                } label: {
                    Image(systemName: "plus")
                }
            }
            .sheet(isPresented: $showingAddExpense) {
                AddView(expenses: expenses)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

AddView:


import SwiftUI

struct AddView: View {
    //
    @State private var name = ""
    @State private var type = "Personal"
    @State private var amount = 0.0

    let types = ["Business", "Personal"]

    @ObservedObject var expenses: Expenses

    var body: some View {
        NavigationView {
            Form {
                TextField("Name", text: $name)

                Picker("Type", selection: $type) {
                    ForEach(types, id: \.self) {
                        Text($0)
                    }
                }

                TextField("Amount", value: $amount, format: .currency(code: "USD"))
                    .keyboardType(.decimalPad)
            }

            .navigationTitle("Add new expense")
        }
    }
}

struct AddView_Previews: PreviewProvider {
    static var previews: some View {
        AddView(expenses: Expenses())
    }
}

1      

Move this stuff:

struct ExpenseItem: Identifiable {
    let id = UUID()
    let name: String
    let type: String
    let amount: Double
}

class Expenses: ObservableObject {
    @Published var items = [ExpenseItem]()
}

outside of your ContentView. The problem is that since you define some types inside the ContentView, that means that AddView doesn't know anything about them. But you are trying to use those types inside AddView so you need to make AddView aware of them.

4      

Put a box in your kitchen and put three pairs of socks in the box.

Then ask your room mate to tell you the colors of the socks in the box in the bathroom.

She might respond "There is no box in the bathroom." This is true because the box is out of scope. The box is available if you're in the kitchen, but not available elsewhere.

As @rooster points out, your ExpenseItem struct was available to the code in the ContentView, but not available to the code in AddView. It was out of scope.

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!

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.