MQ. MQ. - 1 year ago 247
Objective-C Question

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);
else {
NSLog(@"Nothing found");
}failure:^(NSURLSessionDataTask *task, NSError *error) {

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

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

Heres the details of response object:

Thanks in advance for helping me out.

Answer Source

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) {
    // ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download