2009-10-06 6 views
4

에서 단어를 일치되고, 그들은 검색의 예를 제공합니다. "John"을 검색하면 "John Smith"및 "Johnny Rotten"과 일치하지만 "Peach John"또는 "The John"은 일치하지 않습니다.검색은 애플의 코드 예제 중 하나에서 시작

이름을 바꾸면 검색 텍스트를 찾을 수있는 방법이 있습니까? 감사. 대신 rangeOfString:options:를 사용

답변

6

시도 :이 작업을 수행 할 수

for (Person *person in personsOfInterest) { 
    NSRange r = [person.name rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)]; 

    if (r.location != NSNotFound) 
    { 
      [self.filteredListContent addObject:person]; 
    } 
} 

또 다른 방법은 자 NSPredicate를 사용하는 것입니다 :

NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", searchText]; 
//the c and d options are for case and diacritic insensitivity 
//now you have to do some dancing, because it looks like self.filteredListContent is an NSMutableArray: 
self.filteredListContent = [[[personsOfInterest filteredArrayUsingPredicate:namePredicate] mutableCopy] autorelease]; 


//OR YOU CAN DO THIS: 
[self.filteredListContent addObjectsFromArray:[personsOfInterest filteredArrayUsingPredicate:namePredicate]]; 
+0

NSPredicate를 사용하여 엄지 손가락을 뽑습니다. – pxl

+0

완벽하게 작동합니다. 나는 여러 필드 (person.firstName, person.lastName)와 비교할 것이기 때문에'rangeOfString : options'을 사용하는 것을 끝 냈습니다. –

+1

@nevan 당신은 술어 안에 여러 개의 비교를 넣을 수 있습니다; 그것은 SQL WHERE 절과 거의 같습니다 :'[NSPredicate predicateWithFormat : @ "name CONTAINS [CD] % @ 또는 lastName CONTAINS [cd] % @", searchText, searchText]; –

1

-[NSString rangeOfString:options:]과 친구들은 당신이 원하는 무엇인가. ". aString의 첫 번째 항목의 수신기의 위치와 길이을주는 NSRange 구조는, 마스크의 옵션을 모듈로 발견되지 {NSNotFound, 0}aString 경우 반환 또는 (@"") 비어 있습니다."

이 : 그것은 반환

관련 문제