Learn Courses My Dashboard

Nested array help needed

I want to have an image and the title of the image in an array so I can flip through the images with the titles. Like the recipe app.
I thought I could do this to link each image with its title

let imageArray: [[String]] = [["MM1", "memo1"], ["MM2", "memo2"], ["MM3", "memo3"]]

But I get an error with my ForEach statements

                           
                            self.isDetailViewShowing = true
                        }, label: {
                          
                            ZStack{
                                Rectangle()
                                    .foregroundColor(.white)
                                
                                VStack(spacing: 0){
                                    Image(imageArray[index])
                                        .resizable()
                                        .aspectRatio(contentMode: /*@START_MENU_TOKEN@*/.fill/*@END_MENU_TOKEN@*/)
                                        .clipped()
                                    Text(imageArray[index])
                                        .padding(5)
                                        .font(Font.custom("Avenir", size: 15))
                                    
                                }
                                
                            }
                            
                        })
                            .tag(index)

I get these errors “Cannot convert value of type ‘[String]’ to expected argument type ‘String’” and "No exact matches in call to initializer "

Any thoughts?

Say index is 0. Then you would be trying to do this:

Image(["MM1", "memo1"])
Text(["MM1", "memo1"])

["MM1", "memo1"] is clearly not a String, so this won’t work.

You want Image(imageArray[index][0]) and Text(imageArray[index][1] instead. That accesses the elements of the nested array and would give you:

Image("MM1")
Text("memo1")

But it would probably be better to do this with a dictionary instead of an array in the first place.

thanks