how can i check NSNumber is null?
Asked Answered
H

5

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.

Hundredweight answered 24/3, 2014 at 13:26 Comment(1)
Where is the region dictionary coming from? What exactly does the first NSLog print?Wilterdink
M
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)
Mcgray answered 24/3, 2014 at 13:27 Comment(2)
NSInvalidArgumentException', reason: '-[NSNull floatValue]: error is given @McgrayHundredweight
Yep, you should check for NSNull before unboxing.Mcgray
O
2

I've been checking if NSDecimalNumber was null using:

if (decimalNumber == nil)

This is working great for me.

Obie answered 4/7, 2014 at 4:28 Comment(1)
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.Undercast
H
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];
            }
Hundredweight answered 24/3, 2014 at 14:9 Comment(0)
L
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.

Lepto answered 24/3, 2014 at 14:15 Comment(2)
== checks identity, but to check if to objects are equal it might only work in some edge cases (string literals). use [myObject isEqualToNumber:@(0.0)] instead.Dachi
I prefer to check for [myObject isEqual:[NSNull null]] vs. using ==, for consistency.Soppy
K
0
[obj isKindOfClass:[NSNull class]]
Kimberleykimberli answered 1/10, 2015 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.