2

Why doesn't the swift compiler accept the following map closure

let coords = ["5.691663622856139,45.11744852318003","5.691341757774353,45.11710783320859"]
let nodes = coords.map({
   let latlon = $0.componentsSeparatedByString(",")
   let lon = Double(latlon[0])!
   let lat = Double(latlon[1])!
   return CLLocation(latitude: lat, longitude: lon)
})

cannot invoke map with an argument list of type (@noescape (String) throws -> _)

and yet

let nodes:[CLLocation] = coords.map({
   let latlon = $0.componentsSeparatedByString(",")
   let lon = Double(latlon[0])!
   let lat = Double(latlon[1])!
   return CLLocation(latitude: lat, longitude: lon)
})

works well.

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134

1 Answers1

3

Swift compiler (still) gets confused when it comes to type-inference for "complex" closures with shorthand arguments, and needs some help along the following lines:

let coords = ["5.691663622856139,45.11744852318003","5.691341757774353,45.11710783320859"]
let nodes = coords.map() {
    (stringCoordinates: String) -> CLLocation in // <- Just specify input/output

    let latlon = stringCoordinates.componentsSeparatedByString(",")
    let lon = Double(latlon[0])!
    let lat = Double(latlon[1])!
    return CLLocation(latitude: lat, longitude: lon)
}
0x416e746f6e
  • 9,872
  • 5
  • 40
  • 68