我得到一个数组:

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


其中self.tournamentDetails.groups是一个NSArray,请使用来自webRequest的JSON字符串进行构建。

项目有时为空,有时包含对象。因此,我需要一个if语句来检查其是否为空。我尝试了一些不同的事情,例如:

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


问题是,如果我的来自valueForKey的Items对象为空,则itemsArray仍包含一个看起来像这样的对象

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

)

)


当我的Items对象中有项目时,它看起来像这样:

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


表示[itemsArray count]始终等于1或更大,然后在不应该时跳入if语句。

有谁知道我如何创建if语句,如果itemsArray包含“ Items:[]”,它将被跳过,如果itemsArray包含“ Items:[很多对象]”,它将运行?

编辑:解决方案是像这样检查第一个索引if([[[itemsArray objectAtIndex:0] count]!= 0)然后运行代码。

最佳答案

尝试这个:

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

07-28 05:55