给出错误消息:无法推断通用参数“数据”

时间:2019-10-07 11:24:25

标签: swiftui

我尝试了很多事情,但是似乎收到了无法删除的连续错误消息。该代码在下面列出,并且我有以下问题:

  1. 错误在哪里?第二条ForEach语句没有错误,但是第一个结果“无法推断出通用参数'Data'”。
  2. 为什么我不能在此块中使用“ for”语句?这样做时,我会收到一条错误消息
  3. 奇怪的是没有预先定义i和j,但是当我这样做时,我得到了错误消息。为什么?
  4. 我想将各种(8)圆弧段绘制为按钮。我在较早的Swift版本中找到了引用(请参见下面的注释行),但是如何在SwiftUI中创建引用?

对初学者的任何帮助将不胜感激。

struct ContentView: View {

    static let segmentCount = 4
    static let circleCount = 2

    var body: some View {

        let r = CGFloat(75.0)
        let center_x = CGFloat(150.0)
        let center_y = CGFloat(150.0)
        var arc_start = CGFloat(45.0 * CGFloat(Double.pi) / 180.0)

        let arc_length = CGFloat(90.0 * CGFloat(Double.pi) / 180.0)
        var arc_width = CGFloat(25.0)
        let line0Target_x = center_x + r * CGFloat(cos(Double(arc_start)))
        let line0Target_y = center_y + r * CGFloat(sin(Double(arc_start)))
        let line1Target_x = center_x + (r + arc_width) * CGFloat(cos(Double(arc_start + arc_length)))
        let line1Target_y = center_x + (r + arc_width) * CGFloat(sin(Double(arc_start + arc_length)))

        ZStack {
            ForEach(1..<ContentView.circleCount){ j in
                r = CGFloat(25.0 + CGFloat((j) - 1.0) * 25.0)
                ForEach(1..<ContentView.segmentCount){ i in
                    Path { path in
                        arc_start = CGFloat((45.0 + (CGFloat(i) - 1.0) * 90.0)) * CGFloat(Double.pi) / 180.0
                        path.move(to: CGPoint(x: line0Target_x, y: line0Target_y))
                        path.addArc(center: CGPoint(x: center_x, y: center_y), radius: r, startAngle: Angle(radians: Double(arc_start)), endAngle: Angle(radians: Double(arc_start + arc_length)), clockwise: false)
                        path.addLine(to: CGPoint(x: line1Target_x, y: line1Target_y))
                        path.addArc(center: CGPoint(x: center_x, y: center_y), radius: (r + arc_width), startAngle: Angle(radians: Double(arc_start + arc_length)), endAngle: Angle(radians: Double(arc_start)), clockwise: true)
                        path.addLine(to: CGPoint(x: line0Target_x, y: line0Target_y))
                        path.closeSubpath()

//                        let shapeLayer = CAShapeLayer()
//                        shapeLayer.strokeColor = UIColor.red.cgColor
//                        shapeLayer.fillColor = UIColor.blue.cgColor
//                        shapeLayer.path = path.cgPath
                    }

                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码将无法编译,因为您在let闭包中添加了varbody,因此编译器期望使用View。 您可以将vars简单地移到Path闭包内(或者可以创建一些函数来对其进行计算):

struct ContentView: View {
    static let segmentCount = 4
    static let circleCount = 2

    var body: some View {
        ZStack {
            ForEach(1..<ContentView.circleCount){ j in
                ForEach(1..<ContentView.segmentCount){ i in
                    Path { path in
                        let r = CGFloat(25.0 + (CGFloat(j) - 1.0) * 25.0)
                        let center_x = CGFloat(150.0)
                        let center_y = CGFloat(150.0)
                        var arc_start = CGFloat(45.0 * CGFloat(Double.pi) / 180.0)

                        let arc_length = CGFloat(90.0 * CGFloat(Double.pi) / 180.0)
                        var arc_width = CGFloat(25.0)
                        let line0Target_x = center_x + r * CGFloat(cos(Double(arc_start)))
                        let line0Target_y = center_y + r * CGFloat(sin(Double(arc_start)))
                        let line1Target_x = center_x + (r + arc_width) * CGFloat(cos(Double(arc_start + arc_length)))
                        let line1Target_y = center_x + (r + arc_width) * CGFloat(sin(Double(arc_start + arc_length)))

                        arc_start = CGFloat((45.0 + (CGFloat(i) - 1.0) * 90.0)) * CGFloat(Double.pi) / 180.0
                        path.move(to: CGPoint(x: line0Target_x, y: line0Target_y))
                        path.addArc(center: CGPoint(x: center_x, y: center_y), radius: r, startAngle: Angle(radians: Double(arc_start)), endAngle: Angle(radians: Double(arc_start + arc_length)), clockwise: false)
                        path.addLine(to: CGPoint(x: line1Target_x, y: line1Target_y))
                        path.addArc(center: CGPoint(x: center_x, y: center_y), radius: (r + arc_width), startAngle: Angle(radians: Double(arc_start + arc_length)), endAngle: Angle(radians: Double(arc_start)), clockwise: true)
                        path.addLine(to: CGPoint(x: line0Target_x, y: line0Target_y))
                        path.closeSubpath()
                    }
                }
            }
        }
    }
}