当 ? 时,过滤的 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");
}
}
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
?
可能是一个特殊字符。只是逃避它:The
?
might be a special character. just escape it: