TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

ForEach () cleaning

Forums > SwiftUI

Hi! There must be a simpler way to do this, right?

I have like 25 instances of the types "priceLevel", "outside" and "tastings" variables. I can get it to loop like this through the var ratings:[Ratings] array but not the next step to get the actual var in the array, like "priceLevel" and so on.

In my "ContentView()" i declare my "breweriesData" at startup.

What is it I can't get my head around?

Thanks!

struct RatingsView: View {
    // MARK: - properties

    var brewery:Brewery
    var headlineColor:Color = Color("ColorDark")

    var body: some View {

        HStack (alignment: .top) {
            ForEach (brewery.ratings, id:\.id) { section in
                ScrollView (.horizontal, showsIndicators: false) {
                    HStack (alignment: .top) {

                        // Price level
                        HStack (alignment: .top, spacing: 10) {
                            Image("\(section.priceLevel[0])")
                                .font(.system(size: 30))
                                .padding(.top, 5)
                            VStack (alignment: .leading, spacing: 2) {
                                Text("\(section.priceLevel[1])").bold()
                                    .foregroundColor(self.headlineColor)
                                Text("\(section.priceLevel[2])")
                            }.font(.footnote).frame(idealWidth: 100, idealHeight: 200)
                        }
                        .padding(10)
                        .padding(.trailing, 5)
                        .background(Color(.white))
                        .cornerRadius(6)

                        // Outside area
                        HStack (alignment: .top, spacing: 10) {
                            Image("\(section.outside[0])")
                                .font(.system(size: 30))
                                .padding(.top, 5)
                            VStack (alignment: .leading, spacing: 2) {
                                Text("\(section.outside[1])").bold()
                                    .foregroundColor(self.headlineColor)
                                Text("\(section.outside[2])")
                                    //.lineLimit(3)
                            }.font(.footnote).frame(idealWidth: 100, idealHeight: 200)
                        }
                        .padding(10)
                        .padding(.trailing, 5)
                        .background(Color(.white))
                        .cornerRadius(6)

                        // Tastings
                        HStack (alignment: .top, spacing: 10) {
                            Image("\(section.tastings[0])")
                                .font(.system(size: 30))
                                .padding(.top, 5)
                            VStack (alignment: .leading, spacing: 2) {
                                Text("\(section.tastings[1])").bold()
                                    .foregroundColor(self.headlineColor)
                                Text("\(section.tastings[2])")
                            }.font(.footnote).frame(idealWidth: 100, idealHeight: 200)
                        }
                        .padding(10)
                        .padding(.trailing, 5)
                        .background(Color(.white))
                        .cornerRadius(6)
                    }
                }
            }
        }.shadow(color: Color("BlackTransparentLight"), radius: 4, x: 0, y: 0)
    }
}

2      

Can you post some sample data and the struct of Brewery?

2      

Likewise.. if we could see how you have Brewery set up it would help. Straight away the ForEach you have supplied is using brewery.ratings. For that to work the property ratings in brewery would have to be an array for it to loop through. Seeing some other code would help out the answer.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.