-1

Hello Guys I want to combine two Arrays. One Int and one String, because I want to multiply the values of the Int Array.

Following is my code:

struct Zutaten: View{
    
    @State var zutaten: [String]
    @State var menge: [Int]
    
    var body: some View{
        HStack{
            
            ForEach(zutaten, id: \.self) { Zutat in
                Text(zutaten)
            }
                .multilineTextAlignment(.center)
                .navigationBarHidden(true)
        }
    }
}

I want the output to be as followed: var menge var zutat, menge zutat, ....

Could you help me?

The whole Code is as Followed

struct Recipe: View {
    
    @State var Eiweiß: Int = 23
    @State var Kohlenhydrate: Int = 24
    @State var Fett: Int = 13
    @State var Calories: Int = 423
    @State var Portionen: Int = 1
    
    var body: some View {
        ScrollView{
            Image("Burger")
                .resizable()
                .frame(width: .infinity, height: 250)
                .padding(.bottom, 10)
            VStack {
                PortionenAngabe(Portionen: $Portionen)
                
                Werte(Eiweiß: $Eiweiß, Kohlenhydrate: $Kohlenhydrate, Fett: $Fett, Calories: $Calories, Portionen: $Portionen)
                
                Zutaten(zutaten: ["Hack", "Mehl", "Brokkoli"], menge: [100, 200, 300])
            }
        }
        .ignoresSafeArea(.all)
    }
}

struct Zutaten: View{
    
    @State var zutaten: [String]
    @State var menge: [Int]
    
    var body: some View{
        HStack{
            
            ForEach(zutaten, id: \.self) { Zutat in
                Text(Zutat)
            }
                .multilineTextAlignment(.center)
                .navigationBarHidden(true)
        }
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • You have two `String` arrays in your code, which doesn't seem to match your question. The desired output is good, but unless you show what's populating the arrays, it's hard to say which array is which and how to *get to* that result. Also, your current code won't compile at all because you're trying to feed a `[String]` to `Text` – jnpdx Jun 16 '22 at 20:09
  • Sorry just changed the second Array. The Text is show in a HStack in the main View and is called in the main view with the input of the Array. – Marius Jensch Jun 16 '22 at 20:12
  • I think *maybe* you're looking for `zip` – jnpdx Jun 16 '22 at 20:13
  • Am I? :D I am new to coding Swift sorry... how to do the zip then? – Marius Jensch Jun 16 '22 at 20:15
  • 1
    Consider to use **one** object `struct Zutat { let name: String; let menge: Int }`. Multiple arrays are bad practice and pretty risky to maintain. – vadian Jun 16 '22 at 20:15
  • Very much agree with the above comment. – jnpdx Jun 16 '22 at 20:16
  • So how it should look like? – Marius Jensch Jun 16 '22 at 20:18

2 Answers2

2

Erstens (first of all) please name variables with starting lowercase letter. It's pretty confusing (verwirrend) to read the code.

Create a new struct

struct Zutat {
    let name: String
    let menge: Int
}

Then pass whole objects

 Zutaten(zutaten: [Zutat(name: "Hack", menge: 100),
                   Zutat(name: "Mehl", menge: 200),
                   Zutat(name: "Brokkoli", menge: 300)])

And change the Zutaten – which is Ingredients in English – view to something like this

struct Zutaten: View {
    
    let zutaten: [Zutat] // never declare an immutable descendant as @State
    
    var body: some View{
        HStack{
            
            ForEach(zutaten, id: \.name) { zutat in
                Text("\(zutat.menge) \(zutat.name)")
            }
            .multilineTextAlignment(.center)
            .navigationBarHidden(true)
        }
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
0

may be this helps:

struct ZutatenView: View {

    var zutaten: [String] = ["Brokkoli", "Hack", "etc"]
    var menge: [String] = ["200", "100", "100"]

    func getMenge(zutat: String) -> String {
        if let index = zutaten.firstIndex(of: zutat) {
            return menge[index]
        }
        return ""
    }
    
    var body: some View{
        HStack{
            ForEach(zutaten, id: \.self) { zutat in
                let menge = getMenge(zutat: zutat)
                Text("\(menge) \(zutat)")
            }
            .multilineTextAlignment(.center)
            .navigationBarHidden(true)
        }
    } 
}

To zip the 2 arrays: see here How to zip arrays in Swift?

dtm
  • 51
  • 3
  • If the two arrays are mutable or get different inputs at some point, this has a potential crash since you don't check to see if `index` is in a valid range on `menge` – jnpdx Jun 16 '22 at 21:34