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] < [lastGoodLocation 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
ifwhenlastGoodLocationis nil (left-hand side of the||condition), and within thatifblock, you’re calling[lastGoodLocation horizontalAccuracy]on a possibly nil reference. Change the innerifto something likeIt may be that
#setLastGoodLocationactually setslastGoodLocation, 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.