【发布时间】:2014-10-22 21:20:01
【问题描述】:
我正在尝试将一段 Objective-C 代码翻译/转换为 Swift。
这是 Objective-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;
}
这是 Swift:
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
}
这是错误:
我不完全理解编译器试图对我说什么。
“annotationView”类是函数返回类型的子类:MKAnnotationView。
【问题讨论】:
标签: swift compiler-errors return-value