Gami Nilesh
Gami Nilesh

Reputation: 581

how to check if array is null or empty in ios sdk?

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

Upvotes: 4

Views: 3555

Answers (5)

Sam
Sam

Reputation: 198

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

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

Upvotes: 2

cyberlobe
cyberlobe

Reputation: 1783

The best possible way should be:

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

Upvotes: 1

sanjeet
sanjeet

Reputation: 1510

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

}

Upvotes: 5

Vinay Chavan
Vinay Chavan

Reputation: 61

[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.
}

Upvotes: 2

meda
meda

Reputation: 45490

You just check for nil

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

or

if ([data[@"Bonds"] isKindOfClass:[NSNull class]]) {
    NSLog(@"it is null");
}

Upvotes: 7

Related Questions