How to fix âProtocol 'View' can only be used as a generic constraint because it has Self or associated type requirementsâ
2022ë
12ì 1ìŒAbout 2 min
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.1.
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