2011-11-19 3 views
2

NSarray의 모든 "딸"을 추출하고 싶습니다.for 루프에서 배열의 모든 rangeOfString을 추출하는 방법은 무엇입니까?

hello 
hi 
hello daughter 
my goodness 
daughter (someone) 
my daughter 
how are you? 
etc. 

그리고 "딸"를 포함하는 모든 라인을 추출 할 :

는 예를 들어, NSArray를 다음과 같이한다. 저를 올바른 방향으로 가르쳐 주시겠습니까?

내 현재 코드는 다음과 같습니다

NSArray *lines = ...; 
NSArray *filteredLines = [lines filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(SELF contains[c] %@)", searchText]]; 

이 (필요 이전 시스템 버전이 사용을 지원해야하는 경우

for (NSString *mystring in self.array) 
{ 
    if ([mystring rangeOfString:searchText].location!=NSNotFound) 

{ 
    NSUInteger idx = [self.array indexOfObject:mystring]; 
    [self.filterarray addObject: mystring]; 
} 
} 

답변

3
NSArray *lines = ...; 
NSArray *filteredLines = [lines filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *string, NSDictionary *bindings) { 
    return ([string rangeOfString:searchText].location != NSNotFound); 
}]]; 

(나중에 아이폰 OS 4.0 이상이 필요합니다) iOS 3.0 이상)

+0

최소 금액은 얼마입니까? – wagashi

+0

'filteredArrayUsingPredicate :'는 iOS 3.0부터 존재하고'predicateWithBlock :'은 iOS 4.0부터 존재합니다. – Regexident

+0

self.filterarray는 nsmutablearray입니다. 코드에 객체를 추가하려면 어떻게해야합니까? '\t 작성 후 경고 메시지가 나타납니다. self.filterarray = [self.array filteredArrayUsingPredicate : [NSPredicate predicateWithBlock :^BOOL (id 문자열, NSDictionary * 바인딩) { \t \t return ([string rangeOfString : searchText] .location! = NSNotFound); \t}]] '경고 : 호환되지 않는 Objective-C 유형'struct NSArray * ', 예상'구조체 NSMutableArray * ''setFilteredListContent : '의 인수 1을 전달할 때 Objective-C 유형 – wagashi

0
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); 

} 
관련 문제