Type '()' cannot conform to 'View'

What does this error mean?

new to coding not sure what causes this Xcode Error.....

Answered by DTS Engineer in 730325022

View here almost certainly means a SwiftUI view. Type () is another name for type Void, which is the return type of Swift functions or expressions that don't return a meaningful value.

Here's how this can happen. If you have SwiftUI code like this:

struct MyView: View {
    var body: some View {
         HStack {
               Text("abc")
                print("Got here")
         }
    }
}

your body function is building a SwiftUI view out of (essentially) a list of sub-views such as Text. The print statement, though, doesn't return a meaningful value (let alone a View that SwiftUI recognizes), so that line's type is Void aka (), and therefore can't belong to the list of views.

All that's a long way round to tell you that you can't put arbitrary code inside a SwiftUI view, just things that SwiftUI allows.

Of course, there might be other ways you can end up with the same error, but they all would reflect an issue similar to the example above.

Accepted Answer

View here almost certainly means a SwiftUI view. Type () is another name for type Void, which is the return type of Swift functions or expressions that don't return a meaningful value.

Here's how this can happen. If you have SwiftUI code like this:

struct MyView: View {
    var body: some View {
         HStack {
               Text("abc")
                print("Got here")
         }
    }
}

your body function is building a SwiftUI view out of (essentially) a list of sub-views such as Text. The print statement, though, doesn't return a meaningful value (let alone a View that SwiftUI recognizes), so that line's type is Void aka (), and therefore can't belong to the list of views.

All that's a long way round to tell you that you can't put arbitrary code inside a SwiftUI view, just things that SwiftUI allows.

Of course, there might be other ways you can end up with the same error, but they all would reflect an issue similar to the example above.

Could you show the code that causes the error ? That will help give a detailed answer.

Type '()' cannot conform to 'View'
 
 
Q