-1

我有一个NSMutableArray对象,它们属于AdDetail具有一些属性的类(例如 adId、adTitle、adPrice ... 等)。我只想删除那些具有adID = 0. 我怎样才能做到这一点 ?

4

6 回答 6

12

也许更优雅的东西就足够了?

[array removeObjectsInArray:[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"adID == 0"]]];
于 2013-04-03T09:40:01.607 回答
2

使用谓词

NSArray *filtered=[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(adId == 0)"]];

使用快速枚举:

NSMutableArray *newArray=[NSMutableArray new];
for(AdDetail adDetailObj in array){
    if(![[adDetailObj adId] isEqualToString:@"0"]){ //if these are strings, if NSInteger then directly compare using ==
       newArray[newArray.count]=adDetailObj;
   }
}

现在newArray包含除id=0

于 2013-04-03T09:38:11.653 回答
1
NSMutableArray *newArray = [NSMutableArray arrayWithArray:yourArray];

for (int i = 0; i < yourArray.count; i++)
{
     AdDetail *obj = (AdDetail *)[yourArray objectAtIndex:i];
     if (obj.adID == 0)
         [newArray removeObjectAtIndex:i];
}
yourArray = [newArray mutableCopy];
于 2013-04-03T09:37:20.110 回答
1
for(i=0; i < myArray.count; i++)
{
  myClass = [myArray objectAtIndex:i];
  if([myClass.adID isEqualtoString:"0"])// if it it int/NSInteger the write myClass.adID==0
  {
            [myArray removeObjectAtIndex:i];
      i--;
  }
}
于 2013-04-03T09:38:03.803 回答
1

使用以下代码:

int count = array.count;
for(i=0;i<count;i++){

     ADetail *adetail = [array objectAtIndex:i];
     if(adetail.adID = 0){

         [array removeObjectAtIndex:i];
          i--;

     }
     count = array.count;
}
于 2013-04-03T09:40:44.777 回答
0
predicate = @"adID == 0";
newArray = [theArray filterUsingPredicate:aPredicate]
于 2013-04-03T09:41:08.793 回答