"Type of expression is ambiguous without more context" 在 Swift 3 中



我在地图中有MKMapView类型的mapView和一些注释。我正在尝试使用 reduce 计算此注释纬度的中位数,但我收到一个错误,声称:

表达类型不明确,没有更多上下文。

这是我的代码:

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

当使用reduce时,传递给闭包的第一个参数表示部分累积的结果。

在您的情况下,其类型需要与初始值匹配0.0- 它是Double

所以,试试这个:

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

最新更新