如何使用 SwiftUI 在 Xcode 中消除 foreach 循环的歧义 [英] How to disambiguate a foreach loop in Xcode with SwiftUI

查看:33
本文介绍了如何使用 SwiftUI 在 Xcode 中消除 foreach 循环的歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Xcode 上使用 swiftUI 创建一个六边形网格.为了循环创建单个六边形,我使用了 foreach 循环.但是,我在创建创建六边形列的函数时遇到错误:

private func makeCol(C: Int, Bump:Int) ->一些视图{返回 ZStack {ForEach(-5...5, id: \.self) {Ynumber inself.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) ->一些视图{返回 ZStack {ForEach(-5...5, id: \.self) {Ynumber in让 xPlot = (ThreeRooted * self.L * Double(C)) + self.centerWidth让 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 是一种结构,它根据已识别数据的基础集合按需计算视图,不是循环.

如何创建六边形网格?下面是一种可能的方法,我试图向您展示如何使用 ForEach 和 SwiftUI 声明性语法.

导入 SwiftUI结构六边形:形状{func path(in rect: CGRect) ->小路 {路径{(路径)在path.move(to: .init(x: rect.midX, y: rect.minY))path.addLine(to: .init(x: rect.maxX, y: (rect.minY + rect.midY)/2 ))path.addLine(to: .init(x: rect.maxX, y: (rect.maxY + rect.midY)/2 ))path.addLine(to: .init(x: rect.midX, y: rect.maxY))path.addLine(to: .init(x: rect.minX, y: (rect.maxY + rect.midY)/2 ))path.addLine(to: .init(x: rect.minX, y: (rect.minY + rect.midY)/2 ))path.closeSubpath()}}}结构内容视图:查看{var主体:一些视图{VStack(间距:-10){ForEach(0 ..< 3) { j inHStack(间距:5){ForEach(0 ..< 4) { i in六边形().fill(颜色.黄色).覆盖(六边形().stroke(Color.red, lineWidth: 5)).frame(宽:50,高:60)}}HStack(间距:5){ForEach(0 ..< 3) { i in六边形().stroke(Color.blue, lineWidth: 5).frame(宽:50,高:60)}}}}}}struct ContentView_Previews: PreviewProvider {静态 var 预览:一些视图 {内容视图()}}

这是结果

为了简单起见,这里的间距和框架参数是固定的,您最好计算适当的数字.

init(Range, content: (Int) -> Content)

<块引用>

Data为Range,ID为Int,Content符合查看.

您尝试使用

init(Data, id: KeyPath, content: (Data.Element) -> Content)

<块引用>

当内容符合视图时可用.

最后我们不知道什么类型返回您的六边形函数,它可能是 Shape,但 Shape 是协议,这可能会给您带来麻烦.如果是Shape,尝试将返回类型定义为一些Shape.

是的,编译器错误报告在 SwiftUI 中仍然不太有用

I am trying to create a hexagon grid using swiftUI on Xcode. To loop over the creation of the individual hexagons I am using a foreach loop. However I came Across an error when creating a function that creates a column of hexagons:

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))
            }
        }
    }

Error: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions.

To get around this I extracted my location fields in separate variables. However, this gave another error:

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))
            }
        }
    } 

Error: Unable to infer complex closure return type; add explicit type to disambiguate.

How do I give this loop an explicit type?

解决方案

ForEach is a structure that computes views on demand from an underlying collection of identified data, NOT a loop.

How to create hexagonal grid? Below is one possible approach where I tried to show you how to use ForEach and SwiftUI declarative syntax.

import SwiftUI

struct Hexagon: Shape {
    func path(in rect: CGRect) -> Path {
        Path { (path) in
            path.move(to: .init(x: rect.midX, y: rect.minY))
            path.addLine(to: .init(x: rect.maxX, y: (rect.minY + rect.midY) / 2 ))
            path.addLine(to: .init(x: rect.maxX, y: (rect.maxY + rect.midY) / 2 ))
            path.addLine(to: .init(x: rect.midX, y: rect.maxY))
            path.addLine(to: .init(x: rect.minX, y: (rect.maxY + rect.midY) / 2 ))
            path.addLine(to: .init(x: rect.minX, y: (rect.minY + rect.midY) / 2 ))
            path.closeSubpath()
        }
    }
}

struct ContentView: View {
    var body: some View {
        VStack(spacing: -10) {
            ForEach(0 ..< 3) { j in
                HStack(spacing: 5) {
                    ForEach(0 ..< 4) { i in
                        Hexagon()
                            .fill(Color.yellow)
                            .overlay(
                                Hexagon()
                                .stroke(Color.red, lineWidth: 5)
                            )
                            .frame(width: 50, height: 60)
                    }
                }
                HStack(spacing: 5) {
                    ForEach(0 ..< 3) { i in
                        Hexagon()
                            .stroke(Color.blue, lineWidth: 5)
                            .frame(width: 50, height: 60)
                    }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

and here is the result

Spacing and frame parameters are fixed here for simplicity, you are better to calculate proper numbers.

init(Range<Int>, content: (Int) -> Content)

Available when Data is Range, ID is Int, and Content conforms to View.

You tried to use

init(Data, id: KeyPath<Data.Element, ID>, content: (Data.Element) -> Content)

Available when Content conforms to View.

and finally we don't have any idea what type returns your hexagon function, it could be Shape, but Shape is protocol, and that could make your trouble. If it is Shape, try define return type as some Shape.

Yes, compiler error reporting is still not very usable with SwiftUI

这篇关于如何使用 SwiftUI 在 Xcode 中消除 foreach 循环的歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆