过滤后的NSArray不能正常工作?在NSString中



当我尝试过滤字符串数组时,我没有得到具有?使用filteredArrayUsingPredicate:。您可以用?来代替句子的url。结果还是一样的

下面是简化后的代码:
-(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");
    }
}

如果我只寻找@"http://www.google.com",它被过滤得很好。

更正后的代码:

-(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");
    }
}

?可能是一个特殊字符。转义:

@"http://www.google.com\?test=1"

最新更新