使用map从坐标生成一系列SwiftChart对象

时间:2015-05-24 19:46:39

标签: ios swift swiftcharts

我正在尝试迭代坐标数组以从每个坐标创建一个对象。坐标存储为元组(x,y,z,label)

private enum University 
{
     case Uni0,Uni1,Uni2, Uni3
}

let models: [(x: CGFloat, y:CGFloat, r:CGFloat, type: University)] = [
        (246.56, 138.98, 1,.Uni0), (218.33, 132.71, 1,.Uni0), (187.79, 127.48, 1, .Uni0), (150.63, 135.5, 1, .Uni0), (185.05, 152.57, 1, .Uni3), (213.15, 155.71, 1, .Uni1), (252.79, 158.85, 1, .Uni2), (315.77, 150.62, 1, .Uni0), (220.55, 149.57, 1, .Uni3)

生成错误的行

let xValues = models{$0.x}.map{ChartAxisValueInt($0.x,labelSettings: labelSettings)}

ChartAxisValueInt是SwiftCharts的一个类。构造函数采用IntChartLabelSettings的实例。

错误消息显示无法使用类型((_) - > _)的参数列表调用* map。

我相信map是合适的,因为我想通过将函数appyling到另一个系列的每个成员来生成一个序列。

更令人困惑的是,SwiftCharts的以下示例代码不会产生该错误:

let xValues = Array(stride(from: 0, through: 450, by: 50)).map {ChartAxisValueInt($0, labelSettings: labelSettings)}

1 个答案:

答案 0 :(得分:3)

It seems that there is simply a map() call missing,

let xValues = models{ $0.x }.map{ ... }

should be

let xValues = models.map{ $0.x }.map{ ... }

In addition, ChartAxisValueInt() takes an Int as first argument, but your x-coordinates are CGFloats. So this would work:

let xValues = models
    .map { $0.x }
    .map { ChartAxisValueFloat($0, labelSettings: labelSettings) }

However, as @Ixx correctly noticed, the same can be done more effectively in a single mapping:

let xValues = models
    .map { ChartAxisValueFloat($0.x, labelSettings: labelSettings) }