how to use more than two arguments for the ForEach on array.

ForEach with 2 arguments

If I try more than 2 arguments I get error that there are extra arguments.

And even tried using two for each together.

Help me how to use foreach together as I want to do it together.

It would be easier if you pasted code instead of screen captures.

Swift zip accepts only 2 arguments. Read the doc.

but you can chain:

zip( zip( zip(arg1, arg2), arg3), arg4)

Instead of just saying "it didn't work" which give no useful information, please take time to explain:

  • show your code
  • what do you expect ?
  • what do you get ?

I tried this :-

ForEach(Array(zip(zip(menje,menjen),zip(menjen2,menjen3))), id: .0)

                    {

                    (imager,texter) in

                        NavigationLink(destination:lastview(img:imager, texty: texter))

                        {

                            maincustom(content: Image(uiImage:imager), text: texter)

                        }

                    }

got error:-

and as you said I even tried this:-

ForEach(Array(zip(zip(zip(menje,menjen)),menjen2),menjen3), id: .0)

                    {

                    (imager,texter) in

                        NavigationLink(destination:lastview(img:imager, texty: texter))

                        {

                            maincustom(content: Image(uiImage:imager), text: texter)

                        }

                    }

got error:-

What iam trying to do is when going over from first view to last view, I want to display the images in last view related to the image which is clicked in first view with its text, below are som images for better understanding.

First View:-

When clicked an image:-

Last view:-

The image related to it has 3 different images array which Iam printing using scrollview:-

And to print three different images for a particular image I've used different array but in the image above I've just printed image directly in scrollview as an example because Iam unable to iterate over multiple arrays together.

code:-

First View:-

import SwiftUI

struct menjeans: View {

    var menje:[UIImage] =

        [UIImage(named:"IMG_9917")!,

        UIImage(named:"IMG_9920")!,

        UIImage(named:"IMG_9923")!]

    var menjen:[String]=["CROPPED SKINNY JEANS\n₹2,990.00","SKINNY JEANS WITH CHAIN\n₹4,990.00","LOW-RISE SLIM JEANS\n₹2,990.00"]

    var menjen2:[UIImage] =

        [UIImage(named: "IMG_9918")!,

        UIImage(named: "IMG_9922")!,

         UIImage(named: "IMG_9925")!]

    var menjen3:[UIImage] =

        [UIImage(named: "IMG_9919")!,

        UIImage(named: "IMG_9921")!,

         UIImage(named: "IMG_9924")!]

    let layout=[GridItem(.flexible()),GridItem(.flexible())]

    var i=0     var body: some View

    {             ScrollView(.vertical)

            {

            LazyVGrid(columns: layout,spacing: 2)

                {

                    ForEach(Array(zip(menje,menjen)), id: .0)

                    {

                    (imager,texter) in

                        NavigationLink(destination:lastview(img:imager, texty: texter))

                        {

                            maincustom(content: Image(uiImage:imager), text: texter)

                        }

                    }

                    .navigationBarTitle("Men Jeans")

                }

            }

            .navigationBarBackButtonHidden(true)

            .navigationBarItems(leading:

                                    NavigationLink(

                                    destination: clothlist()){

                                            navcustom(content:

                                                       Image(systemName: "line.horizontal.3"),col: .white)

                                    })

        }

struct menjeans_Previews: PreviewProvider {

    static var previews: some View {

        menjeans()

            .environment(.colorScheme, .dark)

    }

}

Last View:-

import SwiftUI

struct lastview: View {

    var img:UIImage

    var texty:String

    var body: some View {

        ScrollView(.vertical){

        ScrollView(.horizontal){

            HStack{

        Image(uiImage: img)

                    .resizable()

                    .frame(width: 500, height: 500, alignment: .center)

        Image(uiImage: img)

                    .resizable()

                    .frame(width: 500, height: 500, alignment: .center)

        Image(uiImage: img)

            .resizable()

            .frame(width: 500, height: 500, alignment: .center)

            }

        }

            VStack(alignment: .leading) {

            Text(texty)

            }         }

    }

}

Please, learn to use the forum better. We don't need this full page images which tell nothing.

And use code formatter to format code.

I tested the chained zip on a small example

let myArray1 = [1, 2, 3]
let myArray2 = [10, 20, 30]
let myArray3 = [100, 200, 300]
let myArray4 = [1000, 2000, 3000]
let ar = zip(zip(zip(myArray1, myArray2), myArray3), myArray4)
for item in ar { print(item)}

and got the expected result:

  • (((1, 10), 100), 1000)
  • (((2, 20), 200), 2000)
  • (((3, 30), 300), 3000)

If you just want to "chain" the arrays, simply do this:

let appended = myArray1 + myArray2 + myArray3 + myArray4 
print("appended", appended)

and get

  • appended [1, 2, 3, 10, 20, 30, 100, 200, 300, 1000, 2000, 3000]

So now, if you want some help on your initial question:

ForEach(Array(zip(zip(menje,menjen),zip(menjen2,menjen3))), id: \.0)
  • tell exactly what are the content menje, menjen, menjen2, menjen3
  • what do you expect to get from the zip ?

Actually i used zip because It worked in my app for iterating over 2 array, but when the number of array increased it is not working I don't know why.

let ar=zip(zip(zip(menje, menjen), menjen2), menjen3) shows error:- Cannot use instance member 'menje' within property initializer; property initializers run before 'self' is available (for every array in it).

As I posted code and what I want before, can you help me that what should I do to iterate over multiple array together and can separate the output with a specific name like:-

ForEach(Array(zip(menje,menjen)), id: \.0)
 {
      (imager,texter) in     // Like this I want to return different array values to other view.
       NavigationLink(destination:lastview(img:imager, texty: texter))
      {
             maincustom(content: Image(uiImage:imager), text: texter)
      }
 }

First View:-

import SwiftUI
struct menjeans: View {
  var menje:[UIImage] =
    [UIImage(named:"IMG_9917")!,
    UIImage(named:"IMG_9920")!,
    UIImage(named:"IMG_9923")!]
  var menjen:[String]=["CROPPED SKINNY JEANS\n₹2,990.00","SKINNY JEANS WITH CHAIN\n₹4,990.00","LOW-RISE SLIM JEANS\n₹2,990.00"]
  var menjen2:[UIImage] =
    [UIImage(named: "IMG_9918")!,
    UIImage(named: "IMG_9922")!,
     UIImage(named: "IMG_9925")!]
  var menjen3:[UIImage] =
    [UIImage(named: "IMG_9919")!,
    UIImage(named: "IMG_9921")!,
     UIImage(named: "IMG_9924")!]
  let layout=[GridItem(.flexible()),GridItem(.flexible())]
  var i=0  var body: some View
  {      ScrollView(.vertical)
      {
      LazyVGrid(columns: layout,spacing: 2)
        {
          ForEach(Array(zip(menje,menjen)), id: .0)
          {
          (imager,texter) in
            NavigationLink(destination:lastview(img:imager, texty: texter))
            {
              maincustom(content: Image(uiImage:imager), text: texter)
            }
          }
          .navigationBarTitle("Men Jeans")
        }
      }
      .navigationBarBackButtonHidden(true)
      .navigationBarItems(leading:
                  NavigationLink(
                  destination: clothlist()){
                      navcustom(content:
                            Image(systemName: "line.horizontal.3"),col: .white)
                  })
    }
struct menjeans_Previews: PreviewProvider {
  static var previews: some View {
    menjeans()
      .environment(.colorScheme, .dark)
  }
}

Last View:-

import SwiftUI
struct lastview: View {
  var img:UIImage
  var texty:String
  var body: some View {
    ScrollView(.vertical){
    ScrollView(.horizontal){
      HStack{
    Image(uiImage: img)
          .resizable()
          .frame(width: 500, height: 500, alignment: .center)
    Image(uiImage: img)
          .resizable()
          .frame(width: 500, height: 500, alignment: .center)
    Image(uiImage: img)
      .resizable()
      .frame(width: 500, height: 500, alignment: .center)
      }
    }
      VStack(alignment: .leading) {
      Text(texty)
      }    }
  }
}

And the images are in old post of view.

And the menje,menjen2,mejen3 contains images and menjen contains text related to the image in menje.

menjen2 and menjen3 contain images related to images in menje or you can say it has relation with the images in menje so I need to print the images in menje,menjen2 and menje3 in last view after the user clicks on the image of menje in first view.

how to use more than two arguments for the ForEach on array.
 
 
Q