使用Float迭代2D数组时出错

时间:2017-06-12 06:25:10

标签: ios arrays swift multidimensional-array swift3

我正在使用2D数组存储所有坐标,我想将每个坐标分配到CGPoint函数中,但我收到此错误:

  

无法下标inout [[Float]]

中的类型值

以下是我的代码:

        var entryPoint: [[Float]] = [[130.6,52.3],[167.5,52.1],[204.5,51.91],[243.6,48.91],[281.16,48.75],[167.5,67.41],[204.5,67.91],[243.6,68.5],[281.16,69.16]]

        for var x in 0..<entryPoint.count
        {
            for var y in 0..<entryPoint[x].count
            {
                print(entryPoint[x][y])
                let circlePath = UIBezierPath(arcCenter: CGPoint(x: entryPoint[x][y],y: entryPoint[x][y]), radius: CGFloat(0.5), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
                let shapeLayer = CAShapeLayer()
                shapeLayer.path = circlePath.cgPath
                shapeLayer.fillColor = UIColor.red.cgColor
                shapeLayer.strokeColor = UIColor.red.cgColor
                shapeLayer.lineWidth = 0.8
            }
        }

2 个答案:

答案 0 :(得分:2)

只需将类型vagrant更改为Float

CGFloat

此外,您可以像这样迭代:

var entryPoint: [[CGFloat]] = [[130.6,52.3],[167.5,52.1],[204.5,51.91],[243.6,48.91],[281.16,48.75],[167.5,67.41],[204.5,67.91],[243.6,68.5],[281.16,69.16]]

答案 1 :(得分:1)

该错误具有误导性问题,CGPoint(x:y:)需要CGFloat作为参数,而您当前正在将Float值转换Float传递给CGFloat将减少该错误

var entryPoint: [[Float]] = [[130.6,52.3],[167.5,52.1],[204.5,51.91],[243.6,48.91],[281.16,48.75],[167.5,67.41],[204.5,67.91],[243.6,68.5],[281.16,69.16]]
for entry in Array(entryPoint.dropFirst()) {
        print(entry[0])
        print(entry[1])
        let circlePath = UIBezierPath(arcCenter: CGPoint(x: CGFloat(entry[0]),y: CGFloat(entry[1]), radius: CGFloat(0.5), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.cgPath
        shapeLayer.fillColor = UIColor.red.cgColor
        shapeLayer.strokeColor = UIColor.red.cgColor
        shapeLayer.lineWidth = 0.8
    }
}

注意:您还可以创建[[CGFloat]]类型的数组,而不是[[Float]],这也会删除此错误。