Note that there are some explanatory texts on larger screens.

plurals
  1. POFiltered NSArray not working right when a ? is in the NSString
    text
    copied!<p>When I try to filter an array of strings I do not get a match for the value that has a ? in it when using filteredArrayUsingPredicate:. You can substitute the URLs for just a sentence with a ? and you still get the same thing.</p> <p>Here's the simplified code:</p> <pre><code>-(void)test { NSArray *theURLs = [NSArray arrayWithObjects:@"http://www.google.com", @"http://www.google.com?test=1", nil]; NSString *currentURL = @"http://www.google.com?test=1"; NSLog(@"currentURL %@", currentURL); NSPredicate *matchURLPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", [currentURL lowercaseString]]; NSLog(@"match predicate %@", matchURLPredicate); NSArray *filteredArray = [theURLs filteredArrayUsingPredicate:matchURLPredicate]; NSLog(@"filtered array %@", filteredArray); if ([filteredArray count]== 0) { NSLog(@"http://www.google.com?test=1 should have been found, but was not"); } else { NSLog(@"http://www.google.com?test=1 was found"); } } </code></pre> <p>If I look for just @"http://www.google.com" it is filtered just fine.</p> <p>Here's the corrected code:</p> <pre><code>-(void)test { NSArray *theURLs = [NSArray arrayWithObjects:@"http://www.google.com", @"http://www.google.com?test=1", nil]; NSString *currentURL = @"http://www.google.com\\?test=1"; NSLog(@"currentURL %@", currentURL); NSPredicate *matchURLPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", [currentURL lowercaseString]]; NSLog(@"match predicate %@", matchURLPredicate); NSArray *filteredArray = [theURLs filteredArrayUsingPredicate:matchURLPredicate]; NSLog(@"filtered array %@", filteredArray); if ([filteredArray count]== 0) { NSLog(@"http://www.google.com?test=1 should have been found, but was not"); } else { NSLog(@"http://www.google.com?test=1 was found"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload