下面的代码在我的var body: some View {
中。当我运行这段代码时,应用程序完全按照我想要的方式构建和运行。问题是,如果我删除其中一个注释行,我会得到"Extra argument in call",如果我删除这两个行的注释,我会得到错误"Extra arguments at positions #11,#12 in call“。我真的只需要调用它11次,我只尝试了12次,看看误差是限制在10还是最后一个值。
为什么这样可以调用BarView() 10次而不是11或12次?我试着把它放到一个1...10循环中,但也不知道如何让它工作。
我使用的是xCode 12.2。我已经能够找到很多和我有相同错误的人,但他们都不是出于相同的原因,所以我无法自己诊断。我真的只是为了好玩而构建应用程序,并且经常在这个过程中跌跌撞撞,我只是真的被困在了这一点上,所以任何帮助都会非常感谢。谢谢。
HStack{
BarView(barGradeValue: 0, barSendValue: 10, barFlashValue: 10)
BarView(barGradeValue: 1, barSendValue: 20, barFlashValue: 10)
BarView(barGradeValue: 2, barSendValue: 30, barFlashValue: 10)
BarView(barGradeValue: 3, barSendValue: 40, barFlashValue: 10)
BarView(barGradeValue: 4, barSendValue: 50, barFlashValue: 10)
BarView(barGradeValue: 5, barSendValue: 60, barFlashValue: 10)
BarView(barGradeValue: 6, barSendValue: 50, barFlashValue: 10)
BarView(barGradeValue: 7, barSendValue: 40, barFlashValue: 10)
BarView(barGradeValue: 8, barSendValue: 30, barFlashValue: 10)
BarView(barGradeValue: 9, barSendValue: 20, barFlashValue: 10)
//BarView(barGradeValue: 10, barSendValue: 10, barFlashValue: 10)
//BarView(barGradeValue: 10, barSendValue: 10, barFlashValue: 10)
}
然后,为了显示它调用的是什么,BarView()是下面的代码。同样,当我调用它10次时,它的工作方式就是我想要的,只是不超过10次。
struct BarView: View {
var barGradeValue: Int
var barSendValue: CGFloat
var barFlashValue: CGFloat
//reference to main struct above
var content = ContentView()
var body: some View {
VStack {
Text(content.gradesV[barGradeValue])
ZStack (alignment: .top){
//Full height
Capsule().frame(width: 20, height: 100)
.foregroundColor(Color.gray)
//Flash Value
Capsule().frame(width: 20, height: barSendValue+barFlashValue)
.foregroundColor(Color.yellow)
//Send Value
Capsule().frame(width: 20, height: barSendValue)
.foregroundColor(content.gradesColor[barGradeValue])
}
}
}
}
发布于 2020-12-19 12:33:20
HStack
(和许多其他视图)接受的闭包是一种称为ViewBuilder
的function builder。最多支持10个View
作为参数。这些都是在SwiftUI模块中硬编码的:
static func buildBlock<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8, _ c9: C9) -> TupleView<(C0, C1, C2, C3, C4, C5, C6, C7, C8, C9)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View, C6 : View, C7 : View, C8 : View, C9 : View
设计者可以进行更多的硬编码,但最终决定不这样做。这可能是因为您可以简化视图的代码,不需要在视图构建器中放置那么多视图。
在这种情况下,您可以使用ForEach
HStack {
ForEach(0..<11) { i in
BarView(barGradeValue: i, barSendValue: i < 6 ? (i + 1) * 10 : (11 - i) * 10, barFlashValue: 10)
}
}
https://stackoverflow.com/questions/65366621
复制相似问题