0

Here is my test for filtering the array by a string. It work well if my string doesn't contain (') character

    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"Nick", @"b'en", @"Adam", @"Melissa", @"arbind", nil];

    //NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'b'"]; -> it work
    NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'b''"]; -> it crash
    NSArray *beginWithB = [array filteredArrayUsingPredicate:sPredicate];
    NSLog(@"beginwithB = %@",beginWithB);

I also try to change my string to 'b\'' or 'b''' but it still crash

Here is the crash log

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "SELF contains[c] 'b'''"'

How to resolve it? Any help would be great appreciated.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Linh
  • 57,942
  • 23
  • 262
  • 279
  • Maybe irrelevant, but `contains` sounds like a bad candidate for a predicate whose results are named `beginWith...` – Alladinian May 19 '16 at 10:56
  • @Alladinian yes, it only my test, I just copy this code from http://stackoverflow.com/a/25738783/5381331. thank you for you correction – Linh May 19 '16 at 10:58

4 Answers4

2

try this

NSString *searchword = @"b";
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchword];

you get output of

enter image description here

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • if you want the excat answer use daskblinkknight answer , else try my answer – Anbu.Karthik May 19 '16 at 11:02
  • you provide a correct solution but the way you show me is not very good. because I ask about the string that a (') character but in your test your string doesn't contains ('). anyway thank you so much for give me a correct solution ;) – Linh May 19 '16 at 11:07
2

Please try to filter result as follows:

    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"Nick", @"b'en", @"Adam", @"Melissa", @"arbind", nil];
        NSString *strToBeSearched = @"b'";

        //NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'b'"]; -> it work

        NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",strToBeSearched]; //-> it also work

        //OR

        NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'b\\''"];


        NSArray *beginWithB = [array filteredArrayUsingPredicate:sPredicate];
        NSLog(@"containB = %@",beginWithB);
Dinesh
  • 1,137
  • 9
  • 14
1

You were pretty close when you tried backslash. This is the character that NSPredicate uses to escape special characters. However, you need two, not one, backslash:

NSMutableArray *array = [NSMutableArray arrayWithObjects:@"Nick", @"b'en", @"Adam", @"Melissa", @"arbind", nil];
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'b\\''"];
//                                                                              ^^
NSArray *beginWithB = [array filteredArrayUsingPredicate:sPredicate];
NSLog(@"beginwithB = %@",beginWithB);

The reason you need two is Objective-C compiler. It processes all string literals in your code, and replaces escape sequences it encounters. If you would like NSPredicate to see a single backslash, your string literal needs to have two backslashes, because backslash itself is encoded as \\ in Objective-C string literals.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

If your name is b'en then,

 NSString *name = @"b'en";
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == \"%@\"", name];

Hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • it crash with the error: `this class is not key value coding-compliant for the key name.'`. I have found the solution. thank for your help – Linh May 19 '16 at 11:12