2

UI: https://i.sstatic.net/X5UhR.jpg

I'm using the ForEach to iterate over an array of minerals in the example code, but I can't find a proper solution to loop the second array (mineral amount) right underneath the minerals.

In different project, I made it so far that the ForEach loops both arrays but for every mineral displays all amount for the Planet and for the second mineral all amount for the Planet and so on.

I did create a new struct with both arrays but without success. Adding a binding property also failed. I hope to learn a new swift method to achieve the desire look.

Data File

import Foundation

struct Planet: Identifiable {
    var id = UUID()
    var name: String
    var minerals: [String]
    var mineralAmount: [String]
}

let planetsData: [Planet] = [

Planet(name: "Jupiter", minerals: ["Iron", "Gold", "Copper"], mineralAmount: ["10K", "20K", "30K"]),
Planet(name: "Earth", minerals: ["Lithium", "Aluminium", "Quartz"], mineralAmount: ["40K", "50K", "60K"])
]

ContentView

import SwiftUI

struct ContentView: View {
    
    var planet: Planet
    var body: some View {
    
      VStack() {
        ForEach(planet.minerals, id: \.self) { item in
            Text(item)
                .font(.system(size: 22, weight: .regular))
                        .foregroundColor(.primary)

                        Text("amount to be added")
                            .font(.system(size: 22, weight: .regular))
                            .foregroundColor(.primary)

                    }
                }
            }
        }
    

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
            ContentView(planet: planetsData[0])
        }
    }


  [1]: https://i.sstatic.net/4MDKs.png
4
  • Actually I haven't got the goal, you want to iterate over planetsData and then over minerals of each planet, right? Commented Dec 19, 2020 at 19:16
  • What do you mean by right underneath the minerals? Are you looking for two different lists on the same page, one for minerals and second for mineral amounts of a single planet? Can you provide some example about the final desired UI? Commented Dec 19, 2020 at 19:31
  • No. I want to iterate over minerals and then over mineral amount. A list showing "Iron" in large and right underneath "10K" in a small font. Commented Dec 19, 2020 at 19:32
  • @emreoktem just added a UI reference Commented Dec 19, 2020 at 19:51

2 Answers 2

4

You can achieve it this way:

ScrollView {
    VStack (alignment: .leading) {
        ForEach(0..<planet.minerals.count) { i in
            HStack {
                Circle()
                    .frame(width: 50, height: 50)
                    .foregroundColor(.black)
                    
                VStack (alignment: .leading) {
                    Text(planet.minerals[i])
                        .font(.system(size: 22, weight: .regular))
                        .foregroundColor(.primary)
                    Text(planet.mineralAmount[i])
                        .font(.system(size: 22, weight: .regular))
                        .foregroundColor(.secondary)
                }
            }
            
        }
        Spacer()
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

why don't you create a dictionary from both values

mineralsDic = [minerals: mineralAmount] 

I of course know the syntax of dictionary but I just try to explain my idea + instead of making 2 loops you can make only one which has less complexity and better performance

3 Comments

This is the correct answer. By having two separate arrays, you have not associated an amount with a mineral. If you were to sort your mineral array at some point, the amount array would not match. By using a dictionary, you can key the amount by the mineral. You can use the dictionary keys as your items in the ForEach and return the amounts with the current iteration of keys.
yeah and you can make your array of keys and loop on it and print both value of key and value with O(n) instead of O(n^2)
@Menaim SwiftUI beginner here Would the ContentView iterate over the mineralsDic in your case? could you provide some code for the solution?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.