Skip to main content

How to fix “Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements”

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to fix “Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements” 관련

SwiftUI by Example

Back to Home

How to fix “Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements” | SwiftUI by Example

How to fix “Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements”

Updated for Xcode 15

This is a particularly confusing problem, and it occurs because SwiftUI relies heavily on opaque return types. The problem happens because of code like this:

struct ContentView: View {
    var body: View {
        Text("SwiftUI")
    }
}

It feels like such a tiny thing, but the body property is declared as returning View rather than some View. The difference is small but important – you can find out more about it in my article How to use opaque return types in Swift 5.1open in new window.

So, to fix the problem use this instead:

struct ContentView: View {
  var body: some View {
    Text("SwiftUI")
  }
}
Similar solutions…
How to fix “Property declares an opaque return type, but has no initializer expression from which to infer an underlying type” | SwiftUI by Example

How to fix “Property declares an opaque return type, but has no initializer expression from which to infer an underlying type”
How to fix “Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type” | SwiftUI by Example

How to fix “Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type”
SwiftUI tips and tricks | SwiftUI by Example

SwiftUI tips and tricks
How to fix “Cannot assign to property: 'self' is immutable” | SwiftUI by Example

How to fix “Cannot assign to property: 'self' is immutable”
How to clip a view so only part is visible | SwiftUI by Example

How to clip a view so only part is visible

이찬희 (MarkiiimarK)
Never Stop Learning.