mac389
mac389

Reputation: 3133

Using map to generate a sequence of SwiftChart objects from coordinates

I am trying to iterate over an Array of coordinates to create an object from each coordinate. The coordinates are stored as tuples (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)

Line that generates the error

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

ChartAxisValueInt is a class from SwiftCharts. The constructor takes an Int and an instance of ChartLabelSettings.

The error message says that *map cannot be invoked with an argument list of type ((_) -> _ ).

I believe map is appropriate because I want to generate one sequence by appyling a function to each member of another series.

More confusingly, the below example code from SwiftCharts does not generate that error:

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

Upvotes: 2

Views: 151

Answers (1)

Martin R
Martin R

Reputation: 539685

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

Upvotes: 3

Related Questions