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

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()
    }
}

3      

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

3      

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.

3      

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()
    }
}

3      

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

3      

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

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.