2

私はswift 1.0では機能しましたが、swift2では機能しなかった関数 geocodeAddressString を使用しました。私のコードの何が問題なのか、どうすれば修正できるのか誰か教えてもらえますか? ありがとうございました!

geocoder.geocodeAddressString(address, {(placemarks: [AnyObject], error: NSError) -> Void in  //Error: Missing argument for parameter 'completionHandler' in call

        if let placemark = placemarks?[0] as? CLPlacemark {
            let annotation = MKPointAnnotation()
            let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
            annotation.coordinate = location
            annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
            annotation.subtitle = "\(StudentArray[student].grade)"
            self.mapView.addAnnotation(annotation)
        }

    })
4

2 に答える 2

2

completionHandlerパラメータ名を指定します。

geocoder.geocodeAddressString(address, completionHandler: { placemarks, error in
    if let placemark = placemarks.first as? CLPlacemark {
        let annotation = MKPointAnnotation()
        let location = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
        annotation.coordinate = location
        annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
        annotation.subtitle = "\(StudentArray[student].grade)"
        self.mapView.addAnnotation(annotation)
    }
})

または、末尾クロージャー構文を使用します ( The Swift Programming LanguageのClosures章のTrailing Closureセクションを参照)。ここでは、 and内からクロージャーを引き出し、 の後に配置します。())

geocoder.geocodeAddressString(address) { placemarks, error in
    if let placemark = placemarks.first as? CLPlacemark {
        let annotation = MKPointAnnotation()
        let location = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
        annotation.coordinate = location
        annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
        annotation.subtitle = "\(StudentArray[student].grade)"
        self.mapView.addAnnotation(annotation)
    }
}
于 2015-07-23T15:17:02.040 に答える
1

完了ハンドラの前に引数を追加します。

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [AnyObject], error: NSError) -> Void in  // Added argument for parameter 'completionHandler' in call

    if let placemark = placemarks?[0] as? CLPlacemark {
        let annotation = MKPointAnnotation()
        let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
        annotation.coordinate = location
        annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
        annotation.subtitle = "\(StudentArray[student].grade)"
        self.mapView.addAnnotation(annotation)
    }

})
于 2015-07-23T14:47:24.453 に答える