使用 swift 中的 map 函数制作 MKPointAnnotations

Using map function in swift to make MKPointAnnotations

我有一个包含 "place" 个对象的数组,其中包含坐标和名称。

例如:

let places = [place(name: "Eiffel Tower", latitude: 48.8582, longitude: 2.2945), place(name: "Statue of Liberty", latitude: 40.6892, longitude: -74.0444), place(name: "Tower of London", latitude: 51.5081, longitude: -0.0761)]

从这个数组中,我想使用地图创建一个新的 MKPointAnnotations 数组。我知道它是这样开始的:

let placeAnnotations = places.map{ (place -> MKPointAnnotation // but that's about all I know for sure!}

...我不知道如何在不犯语法错误的情况下设置 MKPointAnnotation 的 .coordinate 和 .title 属性。谢谢!

是的,您走对了。这是完整的代码:

let places = [place(name: "Eiffel Tower", latitude: 48.8582, longitude: 2.2945), place(name: "Statue of Liberty", latitude: 40.6892, longitude: -74.0444), place(name: "Tower of London", latitude: 51.5081, longitude: -0.0761)]

let annotations = places.map { aPlace -> MKPointAnnotation in
    let annotation = MKPointAnnotation()
    annotation.coordinate = CLLocationCoordinate2D(latitude: aPlace.latitude, longitude: aPlace.longitude)
    return annotation
}

println(annotations)