0

I got an array:

NSArray *itemsArray = [self.tournamentDetails.groups valueForKey:@"Items"];

Where self.tournamentDetails.groups is an NSArray, build with a JSON string from a webRequest.

Items is sometimes empty and sometimes it contains objects. So i need an if statement to check if its empty or not. I've tried some different things like:

if ([itemsArray count]!=0)
if (!itemsArray || !itemsArray.count)

Problem is that if my Items object from valueForKey is empty then the itemsArray still contains an object looking like this

<__NSArrayI 0x178abef0>(
<__NSArrayI 0x16618c30>(

)

)

When theres items inside my Items object it looks like this:

<__NSArrayI 0x18262b70>(
<__NSCFArray 0x181e3a40>(
{
    BirthDate = 19601006T000000;
    ClubName = "Silkeborg Ry Golfklub";
    ClubShortName = "";
    CompletedResultSum =     {
        Actual =         {
            Text = 36;
            Value = 36;
        };
        ToPar =         {
            Text = "";
            Value = 0;
        };
    };
}
)
)

Meaning that [itemsArray count] is always equal to 1 or more, and then it jumps into the if statement when it should not.

Anyone know how i can create an if statement where if itemsArray contains "Items:[]" it will be skipped and if itemsArray contains "Items:[lots of objects]" it will run?

EDIT: Solution is to check up on the first index like this if([[itemsArray objectAtIndex:0] count] != 0) then run code.

14
  • Can you please NSLog the self.tournamentDetails.groups and paste it here (or at least the part where 'Items' appear)? Commented May 19, 2014 at 8:45
  • sure ill update my post Commented May 19, 2014 at 8:46
  • self.tournamentDetails.groups what type is this ? Commented May 19, 2014 at 8:49
  • 1
    So you have array (self.tournamentDetails.groups) with arrays in it?Why do you use valueForKey to take an item from the array?Maybe this is not the best data type. Commented May 19, 2014 at 8:57
  • 1
    After the web request you can build NSDictionary or MyItemsCustomClass to store the objects.As I see from the code above you have an array containing string,dictionary,int. Commented May 19, 2014 at 9:03

2 Answers 2

1

Try this:

if(itemsArray && itemsArray.count>0) //be sure that it has value){
   for(NSArray *item in itemsArray){
       if(item.count > 0){
           // you have an NSDictionary. Will process it 
       }else{
           //item.count == 0 : this is an empty NSArray.
       }
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thx for your answer, i found a more simple solution that worked. If([[itemsArray objectAtIndex:0] count] != 0)
@user2408952: Just my opinion, If([[itemsArray objectAtIndex:0] count] != 0) : just use for 1 item only. In case this array has more than 1 item, I think it will fail. My solution is applied for all cases.
itemsArray && itemsArray.count > 0 is superfluous, just do if (itemsArray.count) { //code }
0

more simpler

  for (id obj in itemsArray)
  {
      if([obj isKindOfClass:[NSArray class]])
      {
          if(obj.count > 0)
          {
             //your if condition here 
          }
      }
  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.