这是 Swift 4 编译器错误吗?

Is this a Swift 4 compiler error?

我写了一个小游乐场来演示如何对数组坐标进行排序以找到最接近的 5 个。我使用高阶函数,将坐标映射到一个也包含距离的结构,对其进行排序,然后选择前 5 个项目。但是,我无法在同一复合语句中选择前 5 名的最后一部分。

代码如下:

    import Foundation
    import CoreLocation

    let currentLatitide = 19.1553902
    let currentLongitude = 72.8528602
    struct CoordStruct: CustomStringConvertible {
        let coord: CLLocationCoordinate2D
        let distance: Double

        var description: String {
            return "lat: " + String(format: "%.4f",coord.latitude) +
                ", long: " + String(format: "%.4f",coord.longitude) + ", distance: " + distance.description
        }
    }

    let location = CLLocation(latitude: currentLatitide, longitude: currentLongitude)

     let points =   [[19.5,71.0],[18.5,72.0],[19.15,72.85],[19.1,75.0],[19.2,70.0],[19.3,70.0],[19.4,70.0],[19.6,70.0],[19.7,70.2],[19.9,70.3],[25,62.0],[24.5,73.4],[23.5,65.0],[21.5,68.0],[20.5,69.0]]

    let structs: [CoordStruct] = points.map //This is where the error shows up.
        {
        let thisCoord = CLLocationCoordinate2D(latitude: [=11=][0], longitude: [=11=][1])
        let thisLocation = CLLocation(latitude:[=11=][0], longitude: [=11=][1])
        let distance = location.distance(from: thisLocation)
        return CoordStruct(coord: thisCoord, distance: distance)
        }
        .sorted { [=11=].distance < .distance }
        //----------------------------------
        .prefix(5)  //This won't compile
        //----------------------------------
    let first5Structs = structs.prefix(5)


    first5Structs.forEach { print([=11=]) }

let test = points.map { [=11=][1] }
    .sorted { [=11=] <  }
    .prefix(5)

print(test)

参见标记为 //This won't compile 的行。取消注释该行后,编译器会在引用 map 语句时说 "ambiguous reference to member 'map'" 。如果我注释掉有问题的行并在单独的行上添加前缀,编译器会很高兴。错误消息毫无意义,这似乎是 Swift 编译器的典型特征,但代码似乎应该编译。我错过了什么?

错误有点误导,问题是由 return 类型不匹配引起的。

  • 注解类型为Array<CoordStruct>
  • prefix returns ArraySlice<CoordStruct>

如果将行更改为

,它会编译
let structs : ArraySlice<CoordStruct> = points.map { ...