Ios 过滤的NSArray在出现故障时无法正常工作?在字符串中
当我尝试筛选字符串数组时,我没有得到与具有?在使用filteredArrayUsingPredicate:时,请在其中输入。您可以将URL替换为一个句子,只需使用?你还是会得到同样的东西 以下是简化代码: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
-(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:需要两次,因为一次被字符串文字吃掉。