Static Analyser Warning on returned doubles?
I am getting a warning from the Xcode 3.2.5 static analyser that I don't quite understand, the warning is:
Warning: The receiver of message 'horizontalAccuracy' is nil and returns a value of type 'CLLocationAccuracy' that will be garbage if([lastGoodLocation horizontalAccuracy] <= DESIREDACCURACY) {
The code compiles and runs fine, but I am just curious as to what is happening and how I might fix it.
@property (nonatomic, retain) CLLocation *lastGoodLocation;
@synthesize lastGoodLocation;
.
// CHECK FOR BEST LOCATION
if(lastGoodLocation == nil || [newLocation horizontalAccuracy] < [lastGoo开发者_高级运维dLocation horizontalAccuracy]) {
NSLog(@"NEWBEST: %0.0fm (%@)", [newLocation horizontalAccuracy], [newLocation timestamp]);
[self setLastGoodLocation:newLocation];
// DESIRED ACCURACY & GEOCODE
if([lastGoodLocation horizontalAccuracy] <= DESIREDACCURACY) {
EDIT:
Can newLocation be returned as nil, this code is in:
locationManager:didUpdateToLocation:fromLocation:
You can get into the outer if
when lastGoodLocation
is nil (left-hand side of the ||
condition), and within that if
block, you're calling [lastGoodLocation horizontalAccuracy]
on a possibly nil reference. Change the inner if
to something like
// DESIRED ACCURACY & GEOCODE
if (lastGoodLocation != nil && ([lastGoodLocation horizontalAccuracy] <= DESIREDACCURACY))
It may be that #setLastGoodLocation
actually sets lastGoodLocation
, but the analyzer may not spot that.
EDIT:
The docs at developer.apple.com suggest that while oldLocation may be nil (on the first fix), newLocation probably shouldn't be -- if location isn't working, I think you'd receive #locationManager:didFailWithError:
instead.
精彩评论