Shivam Tripathi
Shivam Tripathi

Reputation: 1493

Unable to filter results successfully if using OR inside NSPredicate

I have a NSArray & i am trying to filter results based on below mentioned Query :

self->filteredValues =
        [self->AllValues filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.sku contains[cd]%@",text]];

I am able to successfully filter results if i use SELF.sku or SELF.name separately.

Issue is when i use OR between the query like this ,i got the crash result:

 self->filteredValues =
        [self->AllValues filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(SELF.sku contains[cd]%@) OR (SELF.name contains[cd]%@)",text]];

Error : 2018-06-05 12:39:55.137706+0530 AppName[43371:14206450] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't look for value () in string (24" NOBLE WREATH (6)); value is not a string '

How to resolve this out.

Upvotes: 0

Views: 38

Answers (2)

Gleb A.
Gleb A.

Reputation: 1180

Check the predicate's format string. You have two %@ placeholders but only one argument providing value.

I bet this will fix the issue:

[NSPredicate predicateWithFormat:@"(SELF.sku contains[cd]%@) OR (SELF.name contains[cd]%@)", text, text]

Upvotes: 1

Mahendra
Mahendra

Reputation: 8924

There second argument is missing in predicate.

self->filteredValues =
    [self->AllValues filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(SELF.sku contains[cd]%@) OR (SELF.name contains[cd]%@)",text, <#second argument#>]];

Set <#second argument#> in predicate.

Upvotes: 1

Related Questions