Luciano Nascimento
Luciano Nascimento

Reputation: 2600

Filtering Array with NSPredicate

I am trying to filter an array with NSPredicate, but it always returns an empty array.

Code:

_contentFilteredArray = [_contentArray mutableCopy];
[_contentFilteredArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"special == 1"]]; // Always return an empty array.

Original _contentArray:

(
    {
    id = 1;
    special = 0;
},
    {
    id = 2;
    special = 1;
},
    {
    id = 3;
    special = 1;
},
    {
    id = 4;
    special = 0;
    }
)

What I Expect to _contentFilteredArray:

(
    {
    id = 2;
    special = 1;
},
    {
    id = 3;
    special = 1;
},
)

Upvotes: 0

Views: 85

Answers (2)

GoZoner
GoZoner

Reputation: 70135

Make sure that the items in the array are key-value coding compliant for the key special. Does

   [item valueforKey: @"special"]

return the expected numeric value?

Upvotes: 2

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

Try this, more following the traditional withFormat methods.

[NSPredicate predicateWithFormat:@"special == %@", @1]

Upvotes: 1

Related Questions