【问题标题】:Return nil for a struct accessor为结构访问器返回 nil
【发布时间】:2012-05-24 19:16:52
【问题描述】:

对于对象,如果对象无效,我可以编写一个访问器以返回 nil

- (Weather *)howsTheWeather
{
    if (![WeatherNetwork isAvailable]) {
        return nil;
    }
    Weather *weatherNow = [WeatherNetwork getWeather];
    return weatherNow;
}

我想用CLLocationCoordinate2Dstruct 来保存纬度和经度。我试过这个:

- (CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(self.latitude, self.longitude);
    if (!CLLocationCoordinate2DIsValid(coord)) {
        return nil;
    }
    return coord;
}

但是编译器会报错“返回 'void *' from a function with in compatible result type 'CLLocationCoordinate2D'”

处理这种情况的最佳方法是什么?我正在考虑编写一个单独的方法-(BOOL)isLocationValid:(CLLocationCoordinate2D)coordinate。有没有更好的方法来解决这个问题?

【问题讨论】:

    标签: iphone objective-c cocoa-touch struct


    【解决方案1】:

    结构不能为空。您将需要使用选项 2(根据结构包含的数据检查结构的有效性)。或者,您可以将结构包装在 能够被 nil'ed 的伪对象中,但这似乎需要大量工作才能获得虚假收益。

    【讨论】:

      猜你喜欢
      • 2018-11-14
      • 2020-07-25
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      相关资源
      最近更新 更多