SwiftUI:使用ForEach动态加载图像

SwiftUI:使用ForEach动态加载图像,swiftui,swiftui-list,Swiftui,Swiftui List,我有一些图像的名称,如区域,卷,等等 我想使用数据驱动显示的图像。这是一个places可以是什么的示例: let places = { names: ["area", "volume"] } 我在下面尝试了类似的方法,但要在“ForEach”上引用初始值设定项“init(\uu:content:)”,需要“String”符合“可识别的” ForEach(places.name) { place in Nav

我有一些图像的名称,如
区域
,等等

我想使用数据驱动显示的图像。这是一个
places
可以是什么的示例:

   let places = {
      names: ["area", "volume"]
   }
我在下面尝试了类似的方法,但要在“ForEach”上引用初始值设定项“init(\uu:content:)”,需要“String”符合“可识别的”

    ForEach(places.name) { place in
        NavigationLink (destination: Any()) {
            HStack {
                Image(place)

            }

对于像
String
这样不直接符合
identification
的类型,您可以告诉
ForEach
使用什么属性作为
id
。通常使用
字符串
,您会希望使用
.self
(注意,如果
字符串
不是唯一的,这可能会产生有趣的结果)


对于像
String
这样不直接符合
identification
的类型,您可以告诉
ForEach
使用什么属性作为
id
。通常使用
字符串
,您会希望使用
.self
(注意,如果
字符串
不是唯一的,这可能会产生有趣的结果)

struct Places {
    var names : [String]
}

struct ContentView : View {
    let places : Places = Places(names: ["area", "volume"])
    
    var body: some View {
        ForEach(places.names, id:\.self) { place in
                NavigationLink (destination: Text("Detail")) {
                    HStack {
                        Image(place)
                    }
                }
        }
    }
}