How to solve the error: Type '()' cannot conform to 'View' when the problem is inside "if"

Some code is not allowed to be inside an if.

For example how can I call a func inside an if?

Answered by Claude31 in 705150022

why if you add a simple "print("Hello")" we get that alarm??

That's because, in the body, SwiftUI builds views, and only views. And print(…) is a func that returns Void: signature is (), as reported in alert.

So, to do what you want, you need to put in the func: var anothervariable = 0

struct ContentView: View {

    let un = 1
    let deux = 2

    func one() -> some View {
        print("Hello")
        return Text("One")
    }
    func two() -> some View {
        anothervariable = un + deux
        print(anothervariable)
        return Text("Two")
    }

    var body: some View {

        if un > deux {
            one()
        } else {
            two()
        }
   }
}

I don't understand your question. Please show the code.

Of course you can call a func in an if:

struct ContentView: View {

    let un = 1
    let deux = 2
    
    func one() -> some View {
        Text("One")
    }
    func two() -> some View {
        Text("Two")
    }

    var body: some View {

        if un > deux {
            one()
        } else {
            two()
        }
   }
}

but I don't understand why if you add a simple "print("Hello")" we get that alarm??

     if un > deux {
          one()
          print("Hello")
        } else {
          two()
          anothervariable = un + deux
        }

and it's not just with print("Hello"), it's with several things for example:

anothervariable = un + deux

etc.

Accepted Answer

why if you add a simple "print("Hello")" we get that alarm??

That's because, in the body, SwiftUI builds views, and only views. And print(…) is a func that returns Void: signature is (), as reported in alert.

So, to do what you want, you need to put in the func: var anothervariable = 0

struct ContentView: View {

    let un = 1
    let deux = 2

    func one() -> some View {
        print("Hello")
        return Text("One")
    }
    func two() -> some View {
        anothervariable = un + deux
        print(anothervariable)
        return Text("Two")
    }

    var body: some View {

        if un > deux {
            one()
        } else {
            two()
        }
   }
}

like an example, if the only thing I need to do is use "print()"

if I do:

func one() -> some View {
        print("Hello")
        return Text("One")
    }

it will print("Hello") and return Text("One")

is there a way return "nothing" ?, just print()

because if I try to use something like this:

func one() {
        print("Hello")
    }

I have the same error.

is there a way return "nothing" ?, just print()

Yes, return EmptyView() instead of Text()

func one() -> some View {
        print("Hello")
        return EmptyView() //  Text("One")
    }
How to solve the error: Type '()' cannot conform to 'View' when the problem is inside "if"
 
 
Q