Enrique Bozza Dutra
Enrique Bozza Dutra

Reputation: 41

Error message: Generic parameter "FalseContent" could not be inferred

When creating a if I'm getting this error message.

The if checks if a bool, called loggedIn, is true or false.

struct ContentView: View{

    @State var loggedIn = false
    @State var user: BarberUser?
    @State var username: String = ""
    @State var password: String = ""

    var body: some View{

        ZStack{
            Group {
                if !loggedIn {
                    VStack{

                        //a TextField() is here
                        //a SecureField() is here
                        Button(action: { self.loggedIn = true }) {
                            Text("Log in!")
                        }
                     } 
                }else{
                    if self.user?.type = .barber{
                        BarberView()
                    } else {
                        ClientView()
                    }
                }
            }
        }
    }
}

The line that is outputting this error is:

if !loggedIn {

what is the cause of this error? And how can I fix it?

If you need more details about the code ask me and I'll provide.

Edit: added more info to the code.

Upvotes: 2

Views: 345

Answers (1)

Hrabovskyi Oleksandr
Hrabovskyi Oleksandr

Reputation: 3275

The first problem is that SwiftUI frequently show errors at the wrong places. You may try to extract your subviews and it'll make your code clearer and more comfortable to find bugs. The second one, what I see in your code snippet: the compiler should show you:

Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type

if you leave just these lines of code:

struct IfElseStatementsInBody: View {

    @State var loggedIn = false

    var body: some View {

        if !loggedIn {
            VStack {
                Text("Need to login")
                Button(action: { self.loggedIn = true }) {
                    Text("Log in!")
                }
            }
        } else {
            Text("Main view")
        }

    }

}

The common way to avoid it is to wrap your views into AnyView. Remember, body is just a computed variable and it should know, what it need to return. The other way is to embed if...else into another view, like VStack or ZStack

struct IfElseStatementsInBody: View {

    @State var loggedIn = false

    var body: some View {

        if !loggedIn {
            return AnyView(ExtractedView(loggedIn: $loggedIn)) // how to extract subview from here
        } else {
            return AnyView(Text("Main view"))
        }

    }

}

// ... to here
struct ExtractedView: View {

    @Binding var loggedIn: Bool

    var body: some View {
        VStack {
            Text("Need to login")
            Button(action: { self.loggedIn = true }) {
                Text("Log in!")
            }
        }
    }
}

// MARK: embed if...else into ZStack. in this case you'll see changing of
// "loggedIn" variable in the canvas
struct EmbedIfElseStatementsInBody: View {

    @State var loggedIn = false

    var body: some View {

        ZStack {
            if !loggedIn {
                ExtractedView(loggedIn: $loggedIn)// how to extract subview from here
            } else {
                Text("Main view")
            }
        }


    }

}

P.S. hope it'll help. in other way the error is in some other place, but I can't see it now because of lack of code.

Upvotes: 1

Related Questions