我正试图将Objective代码转换为Swift。
以下是目标-C原版:
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *annotationView = nil;
if ([annotation isKindOfClass:[PlaceAnnotation class]])
{
annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
if (annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
}
}
return annotationView;
}
这里是斯威夫特:
func mapView(mapView:MKMapView, annotation:MKAnnotation) {
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("Pin") as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "Pin")
annotationView!.canShowCallout = true
annotationView!.animatesDrop = true
}
return annotationView
}
以下是错误:

我不完全明白编译器想对我说什么。
‘注释视图’类是函数返回类型: MKAnnotationView的子类。
发布于 2014-10-22 21:25:39
您的func签名缺少返回类型:
func mapView(mapView:MKMapView, annotation:MKAnnotation) -> MKPinAnnotationView {如果没有显式的返回类型,swift默认为空元组,这意味着不返回类型--这就是错误消息所说的,可能不是以显式的方式:)
发布于 2014-10-22 21:42:10
下面是编译好的已完成的函数:
func mapView(mapView:MKMapView, annotation:MKAnnotation) -> MKPinAnnotationView {
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("Pin") as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "Pin")
annotationView!.canShowCallout = true
annotationView!.animatesDrop = true
}
return annotationView!
}https://stackoverflow.com/questions/26517334
复制相似问题