使用ScrollView和ForEach时,如下代码所示:
ScrollView(.horizontal,showsIndicators: false) {
HStack {
ForEach(self.QUOTES, id: \.self) { qt in
Text(qt)
}
}
}
没关系,没问题。 但是,当我想在ForEach块中使用任何代码(例如添加图像)时,例如以下代码:
ScrollView(.horizontal,showsIndicators: false) {
HStack {
ForEach(self.QUOTES, id: \.self) { qt in
Text(qt)
Image(systemName: "a.square.fill")
.resizable()
.frame(width: 40, height: 40)
}
}
}
在编写时出现以下错误: 无法推断复杂的闭包返回类型;添加显式类型以消除歧义
答案 0 :(得分:3)
ForEach
结构必须包含一个视图,因此您需要根据希望的布局呈现方式对文本和图像进行分组或将它们嵌入堆栈中。
ScrollView(.horizontal,showsIndicators: false) {
HStack {
ForEach(self.QUOTES, id: \.self) { qt in
Group {
Text(qt)
Image(systemName: "a.square.fill")
.resizable()
.frame(width: 40, height: 40)
}
}
}
}