【问题标题】:Control may reach end of non-void function控制可能到达非空函数的结尾
【发布时间】:2014-05-11 13:14:17
【问题描述】:

我不知道为什么 xcode 会显示此消息,请查看我的代码:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    if (annotation==self.mapView.userLocation) {
        return nil;

        NSString *pinID = @"Salvar";
        MKPinAnnotationView *view = (MKPinAnnotationView*)
        [self.mapView dequeueReusableAnnotationViewWithIdentifier:pinID];

        if (view==nil) {
            view = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:pinID];

            view.canShowCallout = YES;
            view.animatesDrop = YES;

        }
        return view;
    }
}

【问题讨论】:

  • 您的代码中还有另一个问题。在你的第三行你有return nil。这意味着该 if 语句中的任何代码都不会运行,因为它总是在 return nil 处退出整个方法。
  • 我应该把它取下来吗?
  • 最终我无法回答这个问题,因为只有你才能说出你想要的逻辑。如果您想在 annotation == self.mapView.userLocation 时始终返回 nil,那么您可以在返回 nil 后删除 if 块中的所有代码。如果你想运行它下面的所有代码,你应该删除return nil
  • 另外,请注意annotation==self.mapView.userLocation 仅测试两个引用指向内存中的同一位置(两个变量引用完全相同的实例)。如果变量引用了两个在其他方面相等的不同实例,则测试仍将返回 false。我想你可能想要的是if([annotation isEqual:self.mapView.userLocation]) {

标签: iphone objective-c xcode ios5


【解决方案1】:

annotation!=self.mapView.userLocation的情况不提供返回值。

要修复,在第一个条件句中添加一个 else 块,或者在最后的关闭花括号之前无条件地返回一些东西。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    if (annotation==self.mapView.userLocation) {
        // the code that you have already here
    } else {
        return nil; // or whatever you would return if annotation!= userLocation
    }
    // or, instead of the else above...
    return nil;
}

【讨论】:

  • 是的,如果在这种情况下它应该返回的话。讨论指针时归零的正确方法是“nil”。
猜你喜欢
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多