Skip to content

Instantly share code, notes, and snippets.

@drosenstark
Created April 28, 2023 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drosenstark/d90815b9c3ba83a90dbb4bd65a8d70ef to your computer and use it in GitHub Desktop.
Save drosenstark/d90815b9c3ba83a90dbb4bd65a8d70ef to your computer and use it in GitHub Desktop.
How to resolve "Use of protocol 'View' as a type must be written 'any View'" in some cases
// MARK: - Bad: "Use of protocol 'View' as a type must be written 'any View'"
struct LeftRightView: View {
let leftView: View
let rightView: View
var body: some View {
HStack {
leftView
rightView
}
}
}
// MARK: - Good
struct LeftRightView2<Content: View>: View {
let leftView: Content
let rightView: Content
var body: some View {
HStack {
leftView
rightView
}
}
}
@drosenstark
Copy link
Author

image

You cannot solve this without generics

image

@drosenstark
Copy link
Author

Also see this SO question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment