返回双打的静态分析警告?

时间:2011-01-06 22:19:33

标签: iphone objective-c cocoa-touch

我收到Xcode 3.2.5静态分析仪的警告,我不太明白,警告是:

  

警告:消息的接收者   'horizo​​ntalAccuracy'是零和   返回type的值   'CLLocationAccuracy'将是   垃圾                        如果([lastGoodLocation   horizo​​ntalAccuracy]< =   希望之处{/ p>

代码编译并运行良好,但我只是好奇发生了什么以及如何解决它。

@property (nonatomic, retain) CLLocation *lastGoodLocation;

@synthesize lastGoodLocation;

// CHECK FOR BEST LOCATION
if(lastGoodLocation == nil || [newLocation horizontalAccuracy] < [lastGoodLocation horizontalAccuracy]) {
    NSLog(@"NEWBEST: %0.0fm (%@)", [newLocation horizontalAccuracy], [newLocation timestamp]);
    [self setLastGoodLocation:newLocation];

    // DESIRED ACCURACY & GEOCODE
    if([lastGoodLocation horizontalAccuracy] <= DESIREDACCURACY) {

编辑:

newLocation可以返回为nil,此代码位于:

locationManager:didUpdateToLocation:fromLocation:

1 个答案:

答案 0 :(得分:5)

if为零(lastGoodLocation条件的左侧)时,您可以进入外部||,并且在if区块内,您是在可能的nil引用上调用[lastGoodLocation horizontalAccuracy]。将内部if更改为

// DESIRED ACCURACY & GEOCODE
if (lastGoodLocation != nil && ([lastGoodLocation horizontalAccuracy] <= DESIREDACCURACY))

可能#setLastGoodLocation实际上设置了lastGoodLocation,但分析师可能没有发现它。

修改

developer.apple.com处的文档表明虽然oldLocation可能是nil(在第一次修复时),newLocation可能不应该 - 如果位置不起作用,我认为你会收到{{1}而不是。