Post

Replies

Boosts

Views

Activity

Finding the right Protocol for Generic that is interpolated into a String
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!
1
0
851
Oct ’21
Form vs Vstack for SwiftUI for iOS
I'm building a View for account creation, and have a number of text fields (first name, last name, email, etc.). It seems like the natural choice is to embed these TextField() views into a Form, but given that Form is built on UITableView (as I understand it) it becomes challenging and requires some workarounds to add modifiers like .background() and have them perform the way I want them to. If, instead of embedding them in a Form, I just use a VStack, there's no problem. So my question is, is there any reason I should put up with the trouble and use Form? Does it give me any added functionality? Is it considered bad or unclean code to not use Form? From what I can tell, there don't seem to be any real benefits, right?
3
0
2.2k
Jun ’21