0

I want to check if my NSString 'name' contains "Brittany" or "Bob", but the following code doesn't seem to do the trick?

ViewController.m

NSPredicate *p = [NSPredicate predicateWithFormat:@"name contains[cd] %@ OR name contains[cd] %@", @"Brittany", @"Bob"];
NSArray *filtered = [self.messages filteredArrayUsingPredicate:p];

Any idea what this should look like instead? I can't seem to get the syntax right. The issue is that 'filtered' is not returning the arrays in which string name containing "Bob", just the ones in which name contains "Brittany".

Here is what self.messages contains:

 This is the messages data (
            {
            body = "Hi";
            endswaptime = "<null>";
            "first name" = Brittany;
            name = Brittany;
            nid = 1803;
            "node_title" = "Re:";
        },
            {
            body = "It is brittany";
            endswaptime = "<null>";
            "first name" = Brittany;
            name = Brittany;
            nid = 1804;
            "node_title" = "Re:";

        },
            {
            body = "Hellooo :)\n";
            endswaptime = "<null>";
            "first name" = Bob;
            name = "Bob";
            nid = 1805;
            "node_title" = "Re:";


        }
    )
Brittany
  • 1,359
  • 4
  • 24
  • 63
  • Have you tried splitting the single predicate into two separate predicates and combining with NSCompoundPredicate? – A.C. Wright Sep 14 '17 at 02:49
  • I haven't - can you show me what that would look like? Help is appreciated! – Brittany Sep 14 '17 at 02:50
  • 1
    What issue are you having with the code you posted? It looks correct. Of course a lot depends on what kind of data is actually in `self.messages`. Update your question with details about its contents. – rmaddy Sep 14 '17 at 02:50
  • @rmaddy See updated :) – Brittany Sep 14 '17 at 02:55

2 Answers2

0

Perhaps splitting the predicate into two separate predicates and combining with NSCompoundPredicate will help?

NSPredicate *p1 = [NSPredicate predicateWithFormat:@"name contains[cd] %@", @"Brittany"];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"name contains[cd] %@", @"Bob"];
NSCompoundPredicate *p = [NSCompoundPredicate orPredicateWithSubpredicates:@[p1, p2]];
NSArray *filtered = [self.messages filteredArrayUsingPredicate:p];
A.C. Wright
  • 921
  • 9
  • 21
  • I'm thrown the error: "No known class method for selector 'orPredicateWithSubPredicates' on the third line of that block? – Brittany Sep 14 '17 at 03:12
  • Apologies. It should be orPredicateWithSubpredicates. I typed method name off the top of my head. – A.C. Wright Sep 14 '17 at 03:14
  • 1
    @Brittany If this worked then your original code should have worked. Perhaps you had a typo somewhere. – rmaddy Sep 14 '17 at 03:17
  • Glad it worked! I agree with @rmaddy. The original code SHOULD work but I know I have ran into this sort of thing in the past and that is why I suggested it... – A.C. Wright Sep 14 '17 at 03:19
-2

I written in swift language. Try to convert into Objective c. var str = ["Hello", "hel","hello"]

let predicate1 = NSPredicate(format: "SELF contains %@","Hello") let predicate2 = NSPredicate(format: "SELF contains %@","helo") let predicatecompound = NSCompoundPredicate.init(type: .or, subpredicates: [predicate1,predicate2]) print(str.filter { predicatecompound.evaluate(with: $0) })

Reference: Combining 'AND' and 'OR' Condition in NSPredicate

Rajesh
  • 124
  • 9