How to declare a specific protocol in Structure in Swift?

Viewed 146

I am trying to create a structure in Swift:

struct foo: Identifiable {
    var id = UUID()
    var someView: View
}

Here someView is any kind of SwiftUI View struct. But Xcode will raise error

Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements

How to declare a property that follow a specific protocol in Swift?

1 Answers

You may use generics, if that works for you:

struct Foo<T: View>: Identifiable {
    var id = UUID()
    var someView: T
}
Related