I have a SubView that I wrote to avoid redundant code. It has a generic @Binding var
that is presented to the user via string Interpolation (the view will be instantiated with a String or an Int). Because it is used in string interpolation, I get the error
No exact matches in call to instance method 'appendInterpolation'
if I don't have it conform to a protocol that lets swift know that the type can be used in string interpolation.
Doing a little research, I found StringInterpolationProtocol
and ExpressibleByStringInterpolation
as potential candidates, but Int doesn't conform to ExpressibleByStringInterpolation
and String doesn't conform to StringInterpolationProtocol
.
Given that both Strings and Ints can be used in string interpolation, is there another protocol that they both conform to that I can use?
Essentially what I want is to figure out what fits SomeProtocol
here:
struct MySubView<T: SomeProtocol>: View {
@Binding var theVariable: T
var body: some View {
HStack {
Text("This part isn't important")
Text("But this is \(theVariable)")
}
}
Or am I thinking of this the wrong way and there's a better way to go about this? Is an opaque type a better way to go?
Thanks!