ios如何提取for循环中数组的所有rangeOfString?
我想把一个国家安全局的所有“女儿”都挖出来 例如,nsarray是这样制作的:ios如何提取for循环中数组的所有rangeOfString?,ios,for-loop,range,Ios,For Loop,Range,我想把一个国家安全局的所有“女儿”都挖出来 例如,nsarray是这样制作的: hello hi hello daughter my goodness daughter (someone) my daughter how are you? etc. 我想提取所有包含“女儿”的行。你能给我指一下正确的方向吗 我目前的代码是: for (NSString *mystring in self.array) { if ([mystring rangeOfString:searchText].l
hello
hi
hello daughter
my goodness
daughter (someone)
my daughter
how are you?
etc.
我想提取所有包含“女儿”的行。你能给我指一下正确的方向吗
我目前的代码是:
for (NSString *mystring in self.array)
{
if ([mystring rangeOfString:searchText].location!=NSNotFound)
{
NSUInteger idx = [self.array indexOfObject:mystring];
[self.filterarray addObject: mystring];
}
}
(需要iOS 4.0或更高版本)
如果需要支持早期系统版本,请使用以下选项:
NSArray *lines = ...;
NSArray *filteredLines = [lines filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(SELF contains[c] %@)", searchText]];
(需要iOS 3.0或更高版本)这方面的最小iOS是什么?
FilteredarrayingPredicate:
自iOS 3.0以来就存在,而predicateWithBlock:
由于iOS 4.0.self.filterarray是一个nsmutablearray,如何使用代码向其中添加对象?在写入'self.filterarray=[self.array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id字符串,NSDictionary*绑定){return([string rangeOfString:searchText].location!=NSNotFound);}]];''警告:不兼容的Objective-C类型'struct NSArray*',当从不同的Objective-C类型传递'setFilteredListContent:'的参数1时,应为'struct NSMutableArray*'。确定,是否可能不使用predicateWithBlock:?@wagashi:是,请参阅我的更新答案。要将项目添加到self.filterray
中,请使用[self.filterray addObjectsFromArray:[lines filtered…]
NSArray *lines = ...;
NSArray *filteredLines = [lines filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(SELF contains[c] %@)", searchText]];
NSString *textViewText = @“hello hi hello daughter my goodness daughter (someone) my daughter how are you? etc.”;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textViewText];
NSArray *highlightWords = @[@“daughter”,@”hello”];
for(NSString *word in highlightWords)
{
NSRange range = NSMakeRange(0, textViewText.length);
do{
range = [textViewText rangeOfString:word options:NSCaseInsensitiveSearch range:range];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
if (range.location != NSNotFound)
range = NSMakeRange(range.location + 1, textViewText.length - (range.location + 1));
}while(range.location != NSNotFound);
}