1

I got an application which use GPS and display actual location on some labels. Here is the method for updating location:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.longitude];
        latitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.latitude];
    }

    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            [address sizeToFit];
            NSArray *locationArray = [[NSArray alloc] initWithObjects:placemark.thoroughfare,placemark.subThoroughfare,
                                      placemark.postalCode,placemark.locality,placemark.country, nil];
            address.text = [NSString stringWithFormat:@"%@, %@\n%@ %@\n%@",
                                 [locationArray objectAtIndex:0],
                                 [locationArray objectAtIndex:1],
                                 [locationArray objectAtIndex:2],
                                 [locationArray objectAtIndex:3],
                                 [locationArray objectAtIndex:4]];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

Now, sometimes some objects of 'locationArray' are 'null', and the relative labels show me '(null)' on the application, which is not so nice. So I need an 'if' cycle that would check if an object of 'locationArray' is 'null' and, if it is, will not be showed. Any ideas?

UPDATE

I resolved the issue removing the array and using @trojanfoe's method (slightly modified). Here is the code:

- (NSString *)sanitizedDescription:(NSString *)obj {
    if (obj == nil)
    {
        return @"";
    }
    return obj;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    //NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.longitude];
        latitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.latitude];
    }

    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        //NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            [address sizeToFit];

            address.text = [NSString stringWithFormat:@"%@, %@\n%@ %@\n%@",
                            [self sanitizedDescription:placemark.thoroughfare],
                            [self sanitizedDescription:placemark.subThoroughfare],
                            [self sanitizedDescription:placemark.postalCode],
                            [self sanitizedDescription:placemark.locality],
                            [self sanitizedDescription:placemark.country]];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

Thank you so much to all for helping :)

DrKey
  • 3,365
  • 2
  • 29
  • 46
  • 1
    Why do you create the array at all? – trojanfoe Nov 06 '13 at 15:36
  • Cause I'm trying to find the solution on an array, but I can change the code without problems.. – DrKey Nov 06 '13 at 15:39
  • An `NSArray` (and its subclasses) can't (barring some low-level abuse of the API) contain `nil` (which is Objective-C-speak for null), so something else must be going on here. Have you tried to step through the code with the debugger? – Monolo Nov 06 '13 at 15:59
  • It's still not clear why you're putting the placemark strings in an array. If you just want to show the placemark's address formatted nicely, see http://stackoverflow.com/questions/7848291/how-to-get-formatted-address-nsstring-from-addressdictionary. –  Nov 06 '13 at 16:21

2 Answers2

1

You'll have to create a helper method, that tests for the NSNull class and does something different:

- (NSString *)sanitizedDescription:(id)obj {
    if ([obj isKindOfClass:[NSNull class]]) {
        return @"";
    }
    return [obj description];
}

Then call that instead of description directly:

address.text = [NSString stringWithFormat:@"%@, %@\n%@ %@\n%@",
                   [self sanitizedDescription:[locationArray objectAtIndex:0]],
                   [self sanitizedDescription:[locationArray objectAtIndex:1]],
                   [self sanitizedDescription:[locationArray objectAtIndex:2]],
                   [self sanitizedDescription:[locationArray objectAtIndex:3]],
                   [self sanitizedDescription:[locationArray objectAtIndex:4]]];

Note: this method doesn't have to be in self and it doesn't have to be an instance method, it will work just fine as a class method. Perhaps create a helper class full of useful class methods?

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Oh nice, but now the problem are the null objects in the array cause XCode, if an object of 'locationArray' is null, stops with error: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' – DrKey Nov 06 '13 at 16:02
  • @DrKey Yeah you can't put `nul`s into an Objective-C collection class. You'll have to check for `nul` and insert `[NSNull null]` instead. – trojanfoe Nov 06 '13 at 16:13
  • 1
    `NSNull` is a singleton. you can do a much faster comparison using `obj == [NSNull null]` – Brad Allred Nov 06 '13 at 16:19
0

Looking at your code you are using an NSArray and you claim that it is holding NULL which is wrong. NSArray cannot hold a NULL pointer. Something else must be wrong here.

If you really want to store NULL values in an array in objective-c you either need to use a straight C array or use NSPointerArray. Otherwise you need to use [NSNull null] to represent NULL values in an NSArray (as others have pointed out).

Do whichever ever best fits your needs and design. Be aware that NSPointerArray is much easier to work with in iOS 6.0 or later (you tagged as iOS 7 so you should be fine). The pointer array can be used with either strong or weak ARC semantics depending on your needs.

Brad Allred
  • 7,323
  • 1
  • 30
  • 49