SwiftUI · Lists & Forms | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4001741-swiftui/lessons/16

What’s the purpose of .tag($0)?

I checked the documentation but still don’t quite understand
“Sets a tag for the view in order to differentiate it from a list of view options.”

Another issue is the navigation bug. Can’t go to the second page after selecting once.
Runs fine on actual device (13.3.1 iPhone Pro) but the whole list sudden moves upward when you arrive at the page

There are still issues and bugs with SwiftUI which will be ironed out over time. This technology is still very fresh! In the early days when I made the course, we noticed Apple seemed to use unique tags for repeating views to make sure they could be referenced uniquely. This is no longer necessary but is harmless to keep in.

hello, so when doing the
ForEach(0…<treats.count) {
Text(self.treats[$0].name) }

I wanted to test converting the Int State property to an
empty string and see if it would give me the name property of each
Treat in treats arrays instead of an Int. However when adding a
Text view in the bottom of the form and changing the selection in the picker,
the Text view wouldn’t update, so I tried messing with the forEach and did this one instead:

ForEach(treats , id: .name) { treat in
Text(treat.name) }

That gave me the result I wanted of storing the name property selected and showing it in the Text view.
With the same forEach I wanted to see if it would work with an Int like in the beginning forEach, but it doesn’t.
I wanted to know why that is?

The difference is the first example doesn’t use identifiable, therefore we’re using a range to lookup the value. In your example you’re showing the compiler how to find a unique id from each treat, which means you can pass in a literal treat model rather than an Integer. Your way is better, but I used both methods in the course and wanted to show students how to do it this way as well incase they didn’t want to conform to identifiable and use a simple range instead. Laurie

1 Like

Hi, Can I just ask you why did you have to make the demoTreats constant static?
I can also see it is an extension to the type Treat itself. So it becomes a property to the Type itself.
If we create instances of Treat in an array, it would not have worked?
Why did you choose this way?

Many thanks

There was no particular reason not to make them constants as they’re not mutating. As for static it may have been because I wanted to use them in the preview provider for previewing the view. Feel free to change it if you’re going to use the models in a more meaningful way and have a great day! Laurie :smiley: