2013-10-10 99 views
3

我有一個對象的NSArray,並且這些對象有10個屬性。我想對這些對象進行文本搜索。搜索字符串匹配任何屬性的對象的NSArray

我知道如何一次搜索1個屬性,但有沒有簡單的方法來一次搜索所有屬性?

這裏是我的對象具有的屬性列表:

@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) NSString * phone; 
@property (nonatomic, retain) NSString * secondaryPhone; 
@property (nonatomic, retain) NSString * address; 
@property (nonatomic, retain) NSString * email; 
@property (nonatomic, retain) NSString * url; 
@property (nonatomic, retain) NSString * category; 
@property (nonatomic, retain) NSString * specialty; 
@property (nonatomic, retain) NSString * notes; 
@property (nonatomic, retain) NSString * guid; 

如果我搜索「醫生」,我希望看到所有的結果,其中這些屬性的1個或多個具有單詞「醫生」在裏面。例如,如果1個對象具有「醫生」類別,而另一個對象具有「[email protected]」的電子郵件地址,則它們都應顯示在結果中。

回答

14
NSString *searchTerm = @"search this"; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE[cd] %@", searchTerm]; 
NSArray *filtered = [array filteredArrayUsingPredicate:predicate]; 

如果有一個特定的屬性,你可以謂詞更改爲:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.propertyName LIKE[cd] %@", searchTerm]; 

要搜索的所有屬性,你將不得不將它們與邏輯運算符

NSString *query = @"blah"; 
    NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name contains[cd] %@", query]; 
    NSPredicate *predicatePhone = [NSPredicate predicateWithFormat:@"phone contains[cd] %@", query]; 
    NSPredicate *predicateSecondaryPhone = [NSPredicate predicateWithFormat:@"secondaryPhone contains[cd] %@", query]; 
    NSArray *subPredicates = [NSArray arrayWithObjects:predicateName, predicatePhone, predicateSecondaryPhone, nil]; 

    NSCompoundPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates]; 
0

綁定在一起添加matches:方法到您的班級:

- (BOOL)matches:(NSString *)term { 
    if ([self.name rangeOfString:term options:NSCaseInsensitiveSearch| NSDiacriticInsensitiveSearch].location != NSNotFound) { 
     return YES; 
    } else if ([self.phone rangeOfString:term options:NSCaseInsensitiveSearch| NSDiacriticInsensitiveSearch].location != NSNotFound) { 
     return YES; 
    } else if (...) { // repeat for all of the properties 
     return YES; 
    } 

    return NO; 
} 

現在你可以遍歷你的對象逐項檢查:

NSArray *peopleArray = ... // the array of objects 
NSString *someTerm = ... // the search term 
NSMutableArray *matches = [NSMutableArray array]; 
for (id person in peopleArray) { 
    if ([person matches:someTerm]) { 
     [matches addObject:person]; 
    } 
} 
0

而不是單獨硬編碼每個屬性的名稱,你可以得到類的屬性名稱的數組:List of class properties in Objective-C,(假設他們都是字符串,或者你可以檢查每個屬性的類型是否爲NSString,但我不確定如何執行此操作)

然後在要搜索的每個對象上,可以遍歷每個對象的屬性並檢查每個值:

id objectValue = [object valueForKey:[NSString stringWithUTF8String:propertyName]]; 
// let's say the property is a string 
NSString *objectValueString = (NSString *)objectValue; 

然後,你可以檢查你的財產與SEARCHTERM像匹配:

BOOL propertyValueMatchesSearchTerm = [objectValueString rangeOfString:mySearchTermString].location != NSNotFound; 

if (propertyValueMatchesSearchTerm) { 
    // add object to search results array 
}