当 ? 时,过滤的 NSArray 无法正常工作。在 NSString 中

发布于 2024-12-20 09:19:16 字数 1740 浏览 2 评论 0原文

当我尝试过滤字符串数组时,我没有得到带有 ? 的值的匹配项。使用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");

    }

}

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.

Here's the simplified 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");

    }

}

If I look for just @"http://www.google.com" it is filtered just fine.

Here's the corrected 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");

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2024-12-27 09:19:16

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

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

The ? might be a special character. just escape it:

@"http://www.google.com\\?test=1"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文