質問

 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.

役に立ちましたか?

解決

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)

他のヒント

I've been checking if NSDecimalNumber was null using:

if (decimalNumber == nil)

This is working great for me.

            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];
            }

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.

[obj isKindOfClass:[NSNull class]]
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top