我正在尝试针对整个字符串列表检查字符串。我有一个MKMapView,当我得到一堆结果时,我想排除包含字符串列表的所有结果,例如" Object"," Test",&#34 ;实施例&#34 ;.因此,如果某个位置被调用"对象工厂",它将不会显示在地图上。如果调用了另一个字符串"测试区域",它也不会在地图上显示。下面的代码奇怪的是,如果我切换它,它只显示包含"对象","测试","示例"它们是唯一显示的,但是如果我将它切换为除了这些以外的所有字符串显示,它都不起作用,所有字符串包括不需要的字符串显示。
NSArray *arrayOfStrings = [[NSArray alloc] initWithObjects:
@"Object",
@"Test",
@"Example",
nil];
for (NSString *s in arrayOfStrings)
{
if ([item.name rangeOfString:s options:NSCaseInsensitiveSearch].location == NSNotFound) {
NSLog(@"Here it is: %@", s);
NSLog(@"Good String");
[self.pastURLs addObject:item.name];
NSLog(@"Here is pastURLS count: %lu", (unsigned long)self.pastURLs.count);
[mapView addAnnotation:annotation];
}
}
这是什么时候有效,但错误的方式:
NSArray *arrayOfStrings = [[NSArray alloc] initWithObjects:
@"Object",
@"Test",
@"Example",
nil];
for (NSString *s in arrayOfStrings)
{
if ([item.name rangeOfString:s options:NSCaseInsensitiveSearch].location != NSNotFound) {
NSLog(@"Here it is: %@", s);
NSLog(@"Good String");
[self.pastURLs addObject:item.name];
NSLog(@"Here is pastURLS count: %lu", (unsigned long)self.pastURLs.count);
[mapView addAnnotation:annotation];
}
}
答案 0 :(得分:0)
如果在其中找不到任何一个目标字符串,则当前逻辑会在pastURLs数组中添加一个字符串。如果找到NONE目标字符串,则要添加它。试试这个:
NSArray *arrayOfStrings = [[NSArray alloc] initWithObjects:
@"Object",
@"Test",
@"Example",
nil];
BOOL stringFound = NO;
for (NSString *s in arrayOfStrings)
{
if ([item.name rangeOfString:s options:NSCaseInsensitiveSearch].location != NSNotFound) {
// we found something, don't include
stringFound = YES;
}
}
if(!stringFound) {
NSLog(@"Good String");
[self.pastURLs addObject:item.name];
NSLog(@"Here is pastURLS count: %lu", (unsigned long)self.pastURLs.count);
[mapView addAnnotation:annotation];
}