'no exact matches in call to initializer.(I do not know how to fix it)

struct ContentView: View {
    var countries = ["dubai","Dutch","Finland","france","france","Fuji","India","Intaly","Japan","Korean","nepal","pakistan","philippe","Rusia","swiss","Tailand"].shuffled()
    var body: some View {
        VStack {
            ForEach(0...2) { number in Image(self.countries[number])(error here:no exact matches in call to initializer.)
                    .border(Color.black,width:1)
            }
        }
    }
}


Solution 1:[1]

The code below assumes you have image assets named the same as the String Array countries. The problem is you are attempting to initialize a ForEach like you would a for...in. While they are both loops, they are not the same.

I have given example code below, but better practice would be to create a data model struct called "Country" that is Identifiable where one of the parameters is the name of the image. That way you can use one ForEach and show various data on the country, like name, population, geography, etc. var countries would then be typed like this: var countries: [Country] and your ForEach would be simplified.

struct ContentView: View {
    var countries = ["dubai","Dutch","Finland","france","france","Fuji","India","Intaly","Japan","Korean","nepal","pakistan","philippe","Rusia","swiss","Tailand"].shuffled()
    var body: some View {
        VStack {
            // The ForEach will loop through each element and assign it to
            // country for use in the closure where Image() is.
            ForEach(countries, id: \.self) { country in
                Image(country)
                    .border(Color.black,width:1)
            }
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yrb