2

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.

9
  • stackoverflow.com/questions/19557392/… Commented Jun 26, 2015 at 9:25
  • what about if you just set if(responseObject){do next task}else{nothng found} Commented Jun 26, 2015 at 9:27
  • did this it never stops at this condition. Commented Jun 26, 2015 at 9:30
  • What if you change your condition to if (![responseObject isKindOfClass:[NSNull class]] && ![responseObject isKindOfClass:[NSZeroData class]]) ? Commented Jun 26, 2015 at 9:43
  • if (responseObject != nil) is that help? Commented Jun 26, 2015 at 9:43

3 Answers 3

5

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) {
    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that _NSZeroData is just a special NSData that is always 0 length.
0

This is not the fastest response, but it can be of some use to someone.

Here is what I've been using and what you can do:

In some class, say "defaults.h" you can define following:

#define Secure(x) x != (id)[NSNull null] ? x : @""
#define isSecure(x) x != (id) [NSNull null] ? YES : NO

Later, in your method you can use it like this:

if(isSecure(responseObject)){
    doSuccess
}
else {
    doError
}

First method can be used for example for strings, while the other is general.

Comments

-3

Try this

Put this on top

#define isNSNull(value) [value isKindOfClass:[NSNull class]]

And then while comparing

if (isNSNull(object) ...

6 Comments

He states in his question that the value is not NSNull.
if it is not null then he can write it in else part.
If u can check now i have add a picture too
@PradumnaPatil Did you seen the condition which MQ. has defined in his code. If yes then can you please explain what is the difference between your condition and his condition.. As per my knowledge both is doing same thing.. .
Not sure how the else case will help him out
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.