SwiftUI · ForEach & Identifiable | raywenderlich.com


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

can you help explain why you need to switch to ForEach(0..<tips.count) while using the old one still looks the same for Picker

my code:

        Picker(selection: $selectedPickerIndex, label: Text("Relaxation")){
            ForEach(tips) { t in
                HStack {
                    Image(t.imageName).resizable().scaledToFit()
                    Text("Cat relaxation tip: \(t.tip)")
                }
            }
        }

thanks

1 Like

Hello, there were issues when iterating over collection types and relying on a binding. This wasn’t a problem in previous versions of SwiftUI but the version we used for this course requires a Range to work with. Its also good to see different ways of doing things as well. For example, in the final app we build you’ll iterate over bindable collections using Range as well as direct bindings to single objects which is nice to see. :grin:

After checking the next episode, my way above doesn’t provide the selection result :smile: (it won’t give a result hint in Picker inside NavigationView), therefore, isn’t correct

I will catching up to that soon :heart_eyes:

1 Like

Where is the demotips? In the struct you showed it only had the imagename, tips, and UUID, (also i did not download the actual project so maybe you had a model in there)

Hey its in the project folder yes :smiley: Just saves you typing all that stuff out yourself! Laurie

Hi Laurie,

I asked the question a couple of days ago regarding the incorrect text layout of “Select Date” within a date picker. Here was the issue:

Screen Shot 2020-05-18 at 11.02.21 PM

as well as the code:

Screen Shot 2020-05-18 at 11.04.17 PM

I’m now seeing the same issue with the Picker as well with the “Relaxation Actions” text

New_Problem

Here’s the code

New_Code

I tried setting the style of the Picker as well as the DatePicker - and no luck. All the SwiftUI components are being used right of the box - so I’m not sure why the text layout isn’t working. Any insights?

Thanks

Hey @codes this is interesting. It seems like the text is being forced to wrap because there is no room. You’ll either have to reduce the size of the picker or to provide a minimum font scale modifier for the text component, or rotate the device. I believe you can change the frame of the Picker like so:
.frame(width: 150, height: 80, alignment: .center)
The way SwiftUI behaves has changed a little in this instance but it kind of makes sense now that I see it in this example. Let me know how you get on! :grin:

Have you tried putting them in a Form {} ?
I dont know if it helps… though

1 Like

We do cover forms later on @noah8610 yeah