检查数组是否包含if语句无效的数据?

时间:2012-05-30 01:06:49

标签: objective-c ios if-statement

我试图看看我的nsarray是否有数据在灰显单元格文本之前 - 但由于某种原因它正在用可怕的SIGBRT评估if ...如果键'myIndex',下面的代码工作正常存在...当用户在应用程序结束时发送订单时我删除了密钥....所以当应用程序打开订单后我想跳过?????

if(![[[inList objectAtIndex:0] objectForKey:@"myIndex"] count] == 0){

       NSArray *myIndexList = [[inList objectAtIndex:0] objectForKey:@"myIndex"];
       NSLog( @"data from INDEX !!!!!!!! %@", myIndexList);

       for(int n=0; n<[myIndexList count]; n++)
       {
           if(indexPath.row == [[myIndexList objectAtIndex:n] integerValue])
           {
               cell.textLabel.textColor = [UIColor lightGrayColor];
           }
       }

   }

非常感谢您的时间和帮助。

3 个答案:

答案 0 :(得分:0)

您可以检查对象是否存在,如下所示:

if([[inList objectAtIndex:0] objectForKey:@"myIndex")
{
    //It exists, do something (otherwise ignore it)
}

答案 1 :(得分:0)

您需要先确保inList中有一个对象:

if ([inList count] > 0) {
    if([[[inList objectAtIndex:0] objectForKey:@"myIndex"] count] > 0) {
        // there is a valid object
    }
}

答案 2 :(得分:0)

检查NSArray是否包含特定对象的数据:

if ([[NSString stringWithFormat:@"%@", [inList objectAtIndex:0]] length] == 0) {
    //Do somthing if the NSArray object is empty.
} else {
    //Do somthing if the NSArray object countains somthing.
}

检查NSArray中的任何对象是否有数据:

for (int count = 0; count < inList.count; count++) {
    if ([[NSString stringWithFormat:@"%@", [inList objectAtIndex:count]] length] == 0) {
        //Do somthing if the NSArray object is empty.
    } else {
        //Do somthing if the NSArray object countains somthing.
    }
}