1

This might be a basic question, but I am not getting any efficient way to do this. Suppose I have an array of multiple objects containing multiple key value like this. Now, I want the total count of grActual whose value is 16- Jan-13.I can iterate the array, and create a new array with key grAcutal and then I can count. Is there any other way to do that?

    (
    {
    gIActual = "17-Jan-13";
    grActual = "16-Jan-13";
},
    {
    gIActual = "18-Jan-13";
    grActual = "16-Jan-13";
},
    {
    gIActual = "16-Jan-13";
    grActual = "16-Jan-13";      
},
    {
    gIActual = "16-Feb-13";
    grActual = "16-Jan-13";
})  
1
  • You can use Predicate to search objects like NSArray *filteredarray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(grActual == %@)", @"16- Jan-13"]]; which will give you number of objects in the array, the total count of the array would be the Count you need. Commented Jul 9, 2016 at 10:21

1 Answer 1

3

I believe this will do it, not that I use NSPredicate very often:

NSArray *filtered = [array filteredArrayUsingPredicate:
    [NSPredicate predicateWithFormat:@"grActual == %@", @"16-Jan-13"]];
NSUInteger count = [filtered count];

Tip: Store data using the correct type, which for 16-Jan-13 is NSDate, not NSString (not to mention Y2K and localization issues...).

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

4 Comments

It can be searched with NSString using [NSPredicate predicateWithFormat:@"(grActual == %@)", @"16-Jan-13"]
you can't assign in a predicate and the brackets are meaningless in this case so the original was simpler and also correct...
@Wain Yes brackets are meaningless, you can assign using == and it works too.
@Wain Thanks. As you can see I don't use NSPredicate often enough (I am happy with filtering by block, which can do anything).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.