“Type of expression is ambiguous without more cont

2019-07-29 13:32发布

I have mapView of type MKMapView and some annotations in the map. I am trying to calculate the median of the latitude of this annotations using reduce, but I get an error claiming:

Type of expression is ambiguous without more context.

Here is my code:

let medianLatitude = mapView.annotations.reduce( 0.0, { $0.coordinate.latitude + $1.coordinate.latitude })

标签: ios swift swift3
1条回答
做个烂人
2楼-- · 2019-07-29 14:16

When using reduce, the first parameter passed to the closure represents the partially accumulated result.

In your case, its type needs to match the initial value 0.0 -- it's Double.

So, try this:

let medianLatitude = mapView.annotations.reduce( 0.0, { $0 + $1.coordinate.latitude })
查看更多
登录 后发表回答