我有NSMutableArray
个AdDetail
类的对象,这些对象包含一些属性(例如adId,adTitle,adPrice ......等)。我想只删除那些adID = 0
的对象。我怎么能这样做?
答案 0 :(得分:12)
或许更优雅的东西就足够了?
[array removeObjectsInArray:[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"adID == 0"]]];
答案 1 :(得分:2)
使用谓词
NSArray *filtered=[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(adId == 0)"]];
使用fastEnumeration:
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
答案 2 :(得分: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];
答案 3 :(得分: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--;
}
}
答案 4 :(得分: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;
}
答案 5 :(得分:0)
predicate = @"adID == 0";
newArray = [theArray filterUsingPredicate:aPredicate]