![](/img/trans.png)
[英]How to use if/ForEach in a SwiftUI View to show IAP modal?
[英]How to use Foreach on view in SwiftUI?
我正在开发我的第一个 iOS 应用程序,使用 Xcode 11.3 和 Swift5。
但我一直坚持在视图中使用 ForEach。
我想使用 Foreach 将某些struct
放在 ZStack 中,但仍然会出错。
Error : Unable to infer complex closure return type; add explicit type to disambiguate
我不知道如何解决这个问题。 你能帮助我吗?
/* Struct I want to put in */
struct Verses: Identifiable{
var id: Int
var verse : Int
}
/* I want to load all values from struct using foreach in view. */
ZStack {
ForEach(controller.verses) { w in <- Here is where I get error.
Rectangle()
.foregroundColor(Color.white)
.cornerRadius(28)
.opacity(0.4)
.offset(x:0, y:68)
.frame(width:290, height:280)
CardView(date: w.date)
.gesture(DragGesture()
.onChanged({ (value) in
......
ForEach
行必须由单个视图表示,因此您需要类似以下内容(我仍然不确定容器的类型,但仅作为示例)
ForEach(controller.verses) { w in
ZStack {
Rectangle()
.foregroundColor(Color.white)
.cornerRadius(28)
.opacity(0.4)
.offset(x:0, y:68)
.frame(width:290, height:280)
CardView(date: w.date)
.gesture(DragGesture()
.onChanged({ (value) in
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.