2013-10-10 77 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 
}