How to extract the value of CGFloat from CGPoint?

I have an array of CGPoint containing various coordinates. I need to apply the filter to x coordinates and y coordinates separately. I am not sure how to do this the Swift way so I unpack the coordinate using this way currently.

      var xvalues: [CGFloat] = []
      var yvalues: [CGFloat] = []
      if (observation1.count) == 5{
        for n in observation1 {
          xvalues.append(n.x)
          yvalues.append(n.y)
        }
        filter1 = convolve(xvalues, sgfilterwindow5_order2)
        filter2 = convolve(yvalues, sgfilterwindow5_order2)

I am sure there is a more elegant way to do this. How to do this without unpacking the array?

Answered by Claude31 in 684362022
1.       var xvalues: [CGFloat] = []
2.       var yvalues: [CGFloat] = []
3.       if (observation1.count) == 5{
4.         for n in observation1 {
5.           xvalues.append(n.x)
6.           yvalues.append(n.y)
7.         }
8.         filter1 = convolve(xvalues, sgfilterwindow5_order2)
9.         filter2 = convolve(yvalues, sgfilterwindow5_order2)

It seems you miss a closing curly bracket before line 8.

Use map function:

if (observation1.count) == 5 {
    xvalues = observation1.map {$0.x }
    yvalues = observation1.map {$0.y }
}
Accepted Answer
1.       var xvalues: [CGFloat] = []
2.       var yvalues: [CGFloat] = []
3.       if (observation1.count) == 5{
4.         for n in observation1 {
5.           xvalues.append(n.x)
6.           yvalues.append(n.y)
7.         }
8.         filter1 = convolve(xvalues, sgfilterwindow5_order2)
9.         filter2 = convolve(yvalues, sgfilterwindow5_order2)

It seems you miss a closing curly bracket before line 8.

Use map function:

if (observation1.count) == 5 {
    xvalues = observation1.map {$0.x }
    yvalues = observation1.map {$0.y }
}
How to extract the value of CGFloat from CGPoint?
 
 
Q