1

I have an array of objects.Each object has its own properties (name, desc, status). name and desc are NSString and status is BOOL.

I want to filter this array by status property. Ex: Get all objects with status == YES.

How can I achieve this?

2
  • It helps if you provide details of what you have tried. Commented Feb 5, 2014 at 9:14
  • @Kumar, thanks for editing. I'll foresee next time. Commented Feb 5, 2014 at 9:15

3 Answers 3

8

Try use NSPredictate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %@", @"YES"];
NSArray *filterArray = [array filteredArrayUsingPredicate:predicate];

This gives you an array with all object where status is equal YES.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, It works and the answer was first.
1

Try this,

NSPredicate *predicate =[NSPredicate predicateWithFormat:@"status = YES"];
NSArray *filteredArray = [yourArray filteredArrayUsingPredicate:predicate];

6 Comments

why copy other answer?
this is not a copied answer.
You may post correct answer, but check if anyone post as like you. then post your answer or give some more description...
@Ilario This is not a copied answer apparently. See the timestamps, Greg and him posted at the same time. So it might just be a coincidence
@NSNoob yes, that is what happened.. Coincidence!
|
-1

Hi you can try this one,

NSMutableArray *array =[NSMutableArray arrayWithObjects:@"Apple", @"Animal", @"baby", @"ball", nil];

NSPredicate *aPredicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith[a] 'b'"];
NSArray *beginWithA =
    [array filteredArrayUsingPredicate:aPredicate];

The beginWithA array will have { @"Apple", @"Animal" }.

NSPredicate *bPredicate =
    [NSPredicate predicateWithFormat:@"SELF contains[b] 's'"];
[array filterUsingPredicate:bPredicate];
The array will have { @"baby", @"ball" }

Thanks

1 Comment

this does not answer the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.