Hey everyone, so I’m having an issue with using a struct with a generic type, I’ve been using generic types for a bit now, but I still don’t fully understand how they work. So I have this struct called LinkAttribute
:
struct LinkAttribute<Content> where Content : View {
var title: String
@ViewBuilder var destination: Content
}
And Attribute
which can hold different attributes including LinkAttribute
:
struct Attribute {
var structure: Any
}
Now I have this function which gets a list of Attribute
values and must return the Attribute
where its structure is a LinkAttribute
:
static private func getFeedBackAttribute(in attributes: [Attribute]) -> Any? {
for attribute in attributes {
if attribute.structure is LinkAttribute<V> { return attribute.structure }
}
return nil
}
In this function, I’m checking if attribute.structure is LinkAttribute<V>
, and my syntax for LinkAttribute<V>
specifies a dummy struct which conforms to the View
protocol to silence following error:
Generic parameter ‘Content’ could not be inferred in cast to ‘LinkAttribute’
But then my if case doesn’t work, because the view I’ve given to the destination property of LinkAttribute
is not the dummy struct V
… So my question is: What is the right syntax for specifying a struct type with a generic type, solving the problem explained above?