I have an NSMutableArray
of objects which are of AdDetail
class that hold a few properties (for eg. adId, adTitle, adPrice... etc). I want to remove only those objects which have adID = 0
. How can I do that ?
Asked
Active
Viewed 3,893 times
-1

Ankur Arya
- 4,693
- 5
- 29
- 50
-
2use predicate to filter the list/array.. – vishy Apr 03 '13 at 09:37
-
why is everybody down-voting this question? – Ankur Arya Apr 03 '13 at 11:57
-
There are many posts and samples to work out filtering array using NSPredicate.. some of below posts will help u.. http://stackoverflow.com/questions/7207050/searching-filtering-a-custom-class-array-with-a-nspredicate http://stackoverflow.com/questions/3386079/use-nspredicate-to-filter-by-object-attribute – vishy Apr 03 '13 at 09:39
6 Answers
12
Perhaps something more elegant would suffice?
[array removeObjectsInArray:[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"adID == 0"]]];

Zack Brown
- 5,990
- 2
- 41
- 54
-
-
can I negate this?(`[NSPredicate predicateWithFormat:@"adID != 0"]`) – Ankur Arya Apr 03 '13 at 10:12
-
Sure thing :] That would filter the array to remove objects where your adID is not equal to zero. – Zack Brown Apr 03 '13 at 10:21
-
one more thing adID is of class `NSInteger` due to that my app is crashing with `this class is not key value coding-compliant for the key adID.` – Ankur Arya Apr 03 '13 at 10:33
-
1Is your `AdDetail` object a subclass of `NSManagedObject`? Perhaps changing your `NSInteger` to an `NSNumber` would be sufficient? – Zack Brown Apr 03 '13 at 10:51
2
Using predicate
NSArray *filtered=[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(adId == 0)"]];
Using 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;
}
}
Now newArray
contains all objects other than id=0

Anoop Vaidya
- 46,283
- 15
- 111
- 140
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];

Rushi
- 4,553
- 4
- 33
- 46
-
-
4you are removing objects from the array while iterating, this will cause an error – tkanzakic Apr 03 '13 at 09:42
-
-
@tkanzakic: good point, Rushi: you have done edit with your answer but still it will cause an error.. – Ankur Apr 03 '13 at 10:15
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--;
}
}

iPatel
- 46,010
- 16
- 115
- 137
-
3you are removing objects from the array while iterating, this will cause an error – tkanzakic Apr 03 '13 at 09:43
1
Use following code :
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;
}

Payal
- 116
- 3