Search code examples
iosobjective-cobjectafnetworkingnsnull

How to check if (id) object is null in ios?


I have made a function to consume webservices , now I want to check if (id) object is null or not. What I am doing is here:

-(void)callService:(NSString*)request param:(NSString*) parameters completion:(void (^)(NSArray *list, NSError *error))completionHandler
{
    [self.manager POST:request parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {

        NSLog(@"Response Object: %@", responseObject);

        if (![responseObject isKindOfClass:[NSNull class]]) {

            [self methodUsingJsonFromSuccessBlock:responseObject];

            if (completionHandler) {
                //NSLog(@"%@", list);
                completionHandler(list,nil);
            }
        }
        else {
            NSLog(@"Nothing found");
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error) {

        //NSLog(@"Error: %@", [error description]);
        if (completionHandler) {
            completionHandler(nil,error);
        }
    }];
}

but what I have found on break points is when (id) is null it has NSZeroData but no NSNull so it always passes my condition. I am using AFNetworking

Heres the details of response object:

Thanks in advance for helping me out.


Solution

  • Your responseObject is not NSNull, but an NSData (which _NSZeroData is a subclass of).

    So what you really want is this condition:

    if ([responseObject isKindOfClass:[NSData class]] && ((NSData *)responseObject).length > 0) {
        // ...
    }