无法将类型为"CGFloat"的值转换为闭包结果类型"CGPoint"



请考虑以下:

struct Entry {
    let points: [CGPoint]
    func squeezePoints(_ multiplier: CGFloat) -> [CGPoint]{
        return points.map{$0.x * multiplier}
    }
}

由于错误而不编译代码:Cannot convert value of type 'CGFloat' to closure result type 'CGPoint'

您正在尝试将其映射到CGFloat,但将函数的返回类型声明为[CGPoint]。如果要乘以每个点的X坐标,请修改您的功能以映射到CGPoint并保持y未修改。

func squeezePoints(_ multiplier: CGFloat) -> [CGPoint]{
    return points.map{CGPoint(x: $0.x * multiplier, y: $0.y)}
}

最新更新