Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 过滤的NSArray在出现故障时无法正常工作?在字符串中_Ios_Nsarray - Fatal编程技术网

Ios 过滤的NSArray在出现故障时无法正常工作?在字符串中

Ios 过滤的NSArray在出现故障时无法正常工作?在字符串中,ios,nsarray,Ios,Nsarray,当我尝试筛选字符串数组时,我没有得到与具有?在使用filteredArrayUsingPredicate:时,请在其中输入。您可以将URL替换为一个句子,只需使用?你还是会得到同样的东西 以下是简化代码: -(void)test { NSArray *theURLs = [NSArray arrayWithObjects:@"http://www.google.com", @"http://www.google.com?test=1", nil]; NSString *cur

当我尝试筛选字符串数组时,我没有得到与具有?在使用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"

可能是一个特殊字符。逃避它吧:

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

谢谢,我尝试过一次转义,但没有意识到需要两次。@Chad:需要两次,因为一次被字符串文字吃掉。谢谢,我尝试过一次转义,但没有意识到需要两次。@Chad:需要两次,因为一次被字符串文字吃掉。