“没有更多上下文的表达类型是模棱两可的";在斯威夫特 3 [英] "Type of expression is ambiguous without more context" in Swift 3

查看:23
本文介绍了“没有更多上下文的表达类型是模棱两可的";在斯威夫特 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的代码:

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

推荐答案

在使用 reduce 时,传递给闭包的第一个参数表示部分累加的结果.

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

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

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

所以,试试这个:

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

这篇关于“没有更多上下文的表达类型是模棱两可的";在斯威夫特 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆