13
 NSDictionary *regionDict = (NSDictionary *) region;
            NSNumber *lat = [regionDict objectForKey:@"lat"];
            //[latitude addObject:lat];
            NSNumber *lng = [regionDict objectForKey:@"lng"];
            //[longitude addObject:lng];
            NSLog(@"%@%@%@",lat,lng,titles);

            if(lat == [NSNumber numberWithFloat:0.00]  && lng == [NSNumber numberWithFloat:0.00])
            {
                NSLog(@"%@%@%@",lat,lng,titles);
            }
            else
            {
                CLLocationCoordinate2D coord;
                coord.latitude =lat.doubleValue;
                coord.longitude =lng.doubleValue;
                MapViewAnnotation *annotation =[[MapViewAnnotation alloc]initWithTitle:titles AndCoordinate:coord];
                [self.mapView addAnnotation:annotation];
            }

if condition is not satisfied because of NSNumber not check with null value. what is the way i can check? Tell me possible ways.. for checking null.

Priyank Gandhi
  • 626
  • 2
  • 8
  • 21
  • Where is the region dictionary coming from? What exactly does the first NSLog print? –  Mar 24 '14 at 13:40

5 Answers5

18

You can check if an object is null by doing

if ([myObject isKindOfClass:[NSNull class]])

But if you want to check if a float boxed in a NSNumber is zero, you can do

if (lng.floatValue == 0. && lat.floatValue == 0)
Cyrille
  • 25,014
  • 12
  • 67
  • 90
2

I've been checking if NSDecimalNumber was null using:

if (decimalNumber == nil)

This is working great for me.

staticnz
  • 491
  • 1
  • 5
  • 15
  • This works in most cases but not, for instance, when observing a property or Core Data attribute. The change dictionary passed on by observeValueForKeyPath:ofObject:change:context: may contain a null value that will not evaluate to 'nil'. Thus 'NSNumber *oldNumber = [change valueForKey:NSKeyValueChangeOldKey];' will pass the (oldNumber != nil) test but then something like 'oldNumber.integerValue' will throw an exception. – Elise van Looij Apr 06 '17 at 13:00
0
            NSDictionary *regionDict = (NSDictionary *) region;
            NSNumber *lat;
            if([regionDict objectForKey:@"lat"] == [NSNull null])
            {
                lat = 0;
            }
            else
            {
                lat = [regionDict objectForKey:@"lat"];
            }
            NSNumber *lng;
            if([regionDict objectForKey:@"lng"] == [NSNull null] )
            {
                lng=0;
            }
            else
            {
            //[latitude addObject:lat];
                lng = [regionDict objectForKey:@"lng"];
            }
            //[longitude addObject:lng];


            if(lat.floatValue  == 0.00 && lng.floatValue == 0.00)
            {
                NSLog(@"%@%@%@",lat,lng,titles);
            }
            else
            {
                NSLog(@"%@%@%@",lat,lng,titles);
                CLLocationCoordinate2D coord;
                coord.latitude =lat.doubleValue;
                coord.longitude =lng.doubleValue;
                MapViewAnnotation *annotation =[[MapViewAnnotation alloc]initWithTitle:titles AndCoordinate:coord];
                [self.mapView addAnnotation:annotation];
            }
Priyank Gandhi
  • 626
  • 2
  • 8
  • 21
0

I'm not quite clear what you want to check. Depending on your JSON data, you will get one of the following:

myObject == nil - the object isn't there at all. 
myObject == [NSNull null] - the JSON data is "null" without the quotes
myObject.doubleValue == 0.0 - the JSON data contained a number 0 or 0.0

Note that trying to read doubleValue will crash if the JSON data was "null", so that needs checking first. Comparing

myObject == [NSNumber numberWithDouble:0.0]

isn't going to work, because that just compares object pointers. Would be a very huge coincidence if this was actually the same object.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0
[obj isKindOfClass:[NSNull class]]
thatzprem
  • 4,697
  • 1
  • 33
  • 41