4

I want to check if my Bond is empty or null and NULL value.

{
Bonds =(
      {
        DOB = "12/09/1988";
        about = "";
      }
      );
User =     {
    city = CA;
    email = "[email protected]";
};
success = True;
}

Second time this type get data how to check Bond key

{
Bonds = Null;
User =     {
    city = CA;
    email = "[email protected]";
};
success = True;
}
5
  • possible duplicate of What is the right way to check for a null string in Objective-C? Commented Dec 12, 2014 at 9:21
  • Apparently nil and NULL are two different kind of beasts... Commented Dec 12, 2014 at 9:22
  • Please state in which environment you are running in. Commented Dec 12, 2014 at 9:32
  • By the way: you confused "array" with "dictionary" Commented Dec 12, 2014 at 9:39
  • Please sir see my question Commented Dec 13, 2014 at 1:46

5 Answers 5

7

You just check for nil

if(data[@"Bonds"]==nil){
  NSLog(@"it is nil");
}

or

if ([data[@"Bonds"] isKindOfClass:[NSNull class]]) {
    NSLog(@"it is null");
}
Sign up to request clarification or add additional context in comments.

Comments

5
if ([data[@"Bonds"] isKindOfClass:[NSNull class]] || data[@"Bonds"] == nil || [data[@"Bonds"] isEqualToString:@""]) {

}

Comments

2

[NSNull null] always returns a same object so this should work fine.

if (dictionary[@"Bonds"] != [NSNull null]) {
    // requried data is present, now check if it is nil or empty.
}

4 Comments

I think the solution is provided in the wrong implementation language. Null is not the same as NSNUll.
Perfect answer and the recommended way to check for NSNull objects.
@JohanKarlsson: It should be obvious that the output that you saw comes from an NSLog statement, and that's how NSLog logs NSNull objects. Your comment is totally wrong.
Hmmm... looking at the question there is nothing that indicates that this is an output from NSLog. It is clearly JSON output. However looking at the tags I would have noticed that this is Objective-C. So my bad.
2

check: if (![dataArray isKindOfClass:[NSNull class]]) &&

check if it having elements [dataArray firstObject] - to check array having one or more elements.

Comments

1

The best possible way should be:

if (!data[@"Bonds"] || ![data[@"Bonds"] count]){
    NSLog(@"Data Found");
}else{
    NSLog(@"Data Not Found");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.