swift - 如何使用 SwiftUI 在 Xcode 中消除 foreach 循环的歧义
问题描述
我正在尝试在 Xcode 上使用 swiftUI 创建一个六边形网格。为了循环创建单个六边形,我使用了 foreach 循环。然而,我在创建一个创建一列六边形的函数时遇到了一个错误:
private func makeCol(C: Int, Bump:Int) -> some View {
return ZStack {
ForEach(-5...5, id: \.self) {Ynumber in
self.hexagon(
x: (ThreeRooted * self.L * Double(C)) + self.centerWidth,
y: (ThreeRooted * self.L * Double(Ynumber)) + self.centerHeight + (ThreeRootedOverTwo * self.L * Bump))
.stroke(Color.red, lineWidth: CGFloat(self.L/4.0))
}
}
}
错误:编译器无法在合理时间内对该表达式进行类型检查;尝试将表达式分解为不同的子表达式。
为了解决这个问题,我在单独的变量中提取了我的位置字段。但是,这给出了另一个错误:
private func makeCol(C: Int, Bump:Int) -> some View {
return ZStack {
ForEach(-5...5, id: \.self) {Ynumber in
let xPlot = (ThreeRooted * self.L * Double(C)) + self.centerWidth
let yPlot = (ThreeRooted * self.L * Double(Ynumber)) + self.centerHeight + (ThreeRootedOverTwo * self.L * Bump)
self.hexagon(
x: xPlot,
y: yPlot)
.stroke(Color.red, lineWidth: CGFloat(self.L/4.0))
}
}
}
错误:无法推断复杂的闭包返回类型;添加显式类型以消除歧义。
如何给这个循环一个明确的类型?
解决方案
如何给这个循环一个明确的类型?
ForEach(-5...5, id: \.self) { (Ynumber) -> _ExplicitType_ in
...
return self.hexagon(
...
)
}
推荐阅读
- python - 我如何包含来自 cx freeze python 的外部模块
- java - 在回收站视图中列出名称
- oracle - 为什么我的语句因资源繁忙异常而失败?
- directx-11 - 如何在 HLSL 中设置几何着色器?
- python - 如何在模型中将整数字段固定为十个数字输出
- java - 如何找出 Firestore 中交易失败的根本原因?
- python - 无法创建组(文件没有写入意图)
- python - 是否可以下载文件的特定部分?
- java - java.lang.ClassCastException: java.lang.String 无法转换为 java.lang.Long 错误
- javascript - 我可以按城镇限制谷歌地理编码器结果吗?