NEW: Learn to build the incredible iOS 15 Weather app today! >>

Error "Generic parameter 'Content' could not be inferred". iExpense app

Forums > 100 Days of SwiftUI

I thought I followed the tutorial pretty closely, but I am getting the previous error at the .toolbar line. Can anyone help?

import SwiftUI

struct ContentView: View {
    @StateObject var expenses = Expenses()

    @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) {
                if #available(iOS 15.0, *) {
                    AddView(expenses: expenses)
                } else {
                    // Fallback on earlier versions
                }
            }
        }
    }
    func removeItems(at offsets: IndexSet) {
        expenses.items.remove(atOffsets: offsets)
    }
}

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

   

.sheet(isPresented: $showingAddExpense) {
                if #available(iOS 15.0, *) {
                    AddView(expenses: expenses)
                } else {
                    // Fallback on earlier versions
                }
            }

I think this is the place where the compiler can't infer the type of view.

On the one hand your code might return an AddView(expenses:) type of view. On the other hand, it might return a view generated by the commented out section. For argument, call it a Text().

AddView(expenses:) and Text() are two different types of views? Perhaps the compiler doesn't like this. Honestly, your code looks like mine, except for this part!

One way to test, comment out this whole section except for a simple view, such as a Color, or a Text. Does the error go away?

How to combine both types of views? I'm not sure, is this where the Group{} structure is useful? Something happens inside the Group{}, but the sheet only sees ONE type of view returned--the Group type of view!

   

Hi @ndnyzed

Not sure why you put if #available(iOS 15.0, *) { }

  1. Remove the if #available statement.
  2. If you get an error say need to target iOS 15 then change it to that in General>Deployment Info.

The reason is that .sheet(isPresented: Binding<Bool>, content: () -> View) requires a view for all cases but you only have the one and the other has no view (only a comment). So if the device is not on iOS 15 it will fail! So the complier will not build it.

   

Yeah, it must be new for xcode 13 or 13.1. A fallback if someone hasn't updated to the new iOS. It won't compile without it. I was able to take it out from that particular line by putting @available(iOS 15.0, *) at the top, but I am still getting the same error, and a new one: "Cannot find 'expenses' in scope" on the line 5th from the bottom. I dont understand, because isn't it declared right at the top @StateObject var expenses = Expenses()???

import SwiftUI

@available(iOS 15.0, *)
struct ContentView: View {
    @StateObject var expenses = Expenses()

    @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)
                }
            }
        }
    }
    func removeItems(at offsets: IndexSet) {
        expenses.items.remove(atOffsets: offsets)
    }

@available(iOS 15.0, *)
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

   

Your removeItems functions is outside the ContentView so the available annotation doesn't apply to it. Reindent your code to see it better.

   

thank you @roosterboy. That got rid of the new error, but the original still remains

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

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