【问题标题】:"Type of expression is ambiguous without more context" in Swift 3Swift 3 中的“表达式类型在没有更多上下文的情况下是模棱两可的”
【发布时间】:2017-08-11 02:17:44
【问题描述】:

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

在没有更多上下文的情况下,表达式的类型是模棱两可的。

这是我的代码:

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

【问题讨论】:

    标签: ios swift swift3


    【解决方案1】:

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

    在你的情况下,它的类型需要匹配初始值0.0——它是Double

    那么,试试这个:

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

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2021-08-05
      • 1970-01-01
      相关资源
      最近更新 更多