0

As SwiftUI View body attribute has a opaque type 'some View' so it should not allow two concrete types to be returned. But in my case, it's not giving error.

struct TestOneView: View {
    var body: some View {
        if 1 != 1 {
            Text("Text")
        } else {
            Color.blue
        }
    }
}

Now if I change this code to below one, It gives error as expected.

struct TestOneView: View {
    var body: some View {
        if 1 != 1 {
            return Text("Text")
        } else {
            return Color.blue
        }
    }
}

This code gives error 'Function declares an opaque return type 'some View', but the return statements in its body do not have matching underlying types'

Please guide.

Aaban Tariq Murtaza
  • 1,155
  • 14
  • 16
  • The ```body``` of a ```View``` is implicitly a ```ViewBuilder```. I think this change came in with iOS 14. So I guess the error (or lack of one) is all to do with the semantics of a ```ViewBuilder```. – Benzy Neez Jul 23 '23 at 13:25
  • Note that in the first case, you are not explicitly "returning" anything. – Sweeper Jul 23 '23 at 13:34

0 Answers0