2013-02-08 38 views
1

我用不同的搜索組合創建了一個應用程序。檢查多個搜索組合的最佳方法

該應用程序有6個文本框來放置文本和搜索。每個文本字段都用於其他搜索條件。

只有1個文本框需要填寫,然後才能搜索。除此之外,用戶可以選擇填寫和搜索多少個文本框。所以有多種搜索組合可能。

問題是,什麼是確定填充什麼文本字段以及如何搜索滿足搜索條件的對象的最佳方法。

搜索方法應循環對象數組(在我的情況下,僱員對象數組)並檢查值是否匹配。

我的目標是限制if語句的數量。

更新:

這裏是我的代碼至今:

-(IBAction)SearchEmployees:(id)sender{ 

NSString *fullName = [(textfieldName.text)uppercaseString]; 
NSString *functionName = [(textfieldFunction.text)uppercaseString]; 
NSString *department = [(textfieldDepartment.text)uppercaseString]; 
NSString *field = [(textfieldField.text)uppercaseString]; 
NSString *expertise = [(textfieldExpertise.text)uppercaseString]; 
NSString *interest = [(textfieldInterest.text)uppercaseString]; 

NSMutableDictionary *filledTextfields = [NSMutableDictionary dictionary]; 

if (![fullName isEqualToString:@""]){ 
    [filledTextfields setObject: fullName forKey: @"fullName"]; 
} 

if (![functionName isEqualToString:@""]){ 
    [filledTextfields setObject: functionName forKey: @"functionName"]; 
} 

if (![department isEqualToString:@""]){ 
    [filledTextfields setObject: department forKey: @"department"]; 
} 

if (![field isEqualToString:@""]){ 
    [filledTextfields setObject: field forKey: @"field"]; 
} 

if (![expertise isEqualToString:@""]){ 
    [filledTextfields setObject: expertise forKey: @"expertise"]; 
} 

if (![interest isEqualToString:@""]){ 
    [filledTextfields setObject: interest forKey: @"interest"]; 
} 


NSMutableArray *foundEmployee = [[NSMutableArray alloc]init]; 

for (id key in filledTextfields) 
{ 
    NSLog(@"KEY: %@ OBJECT: %@", key, [filledTextfields objectForKey:key]); 

    for (Employee *employee in self.employees){ //self.employees is the array to search in 
     //do something 
    } 

} 

回答

2

我會去與一個NSMutableDictionary,加上陣列到字典中的內容,並使用適當的搜索詞作爲字典的鍵,然後當你搜索某些東西的時候,你可以從文本字段中取出文本,並執行[dictionary objectForKey:textfieldText],它將返回適當的對象,或者如果該搜索詞沒有對象,則返回nil。

即時通訊不完全確定您的搜索條件如何工作,但這可能工作。

1

我將建立一個「化合物謂詞」到陣列篩選:

NSMutableArray *predicates = [NSMutableArray array]; 
if ([textFieldName.text length] > 0) { 
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"fullName =[c] %@", textFieldName.text]; 
    [predicates addObject:pred]; 
} 
if ([textfieldFunction.text length] > 0) { 
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"functionName =[c] %@", textfieldFunction.text]; 
    [predicates addObject:pred]; 
} 
// ... same procedure for remaining search criteria ... 

if ([predicates count] > 0) { // At least one search criterion 
    NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates]; 
    NSArray *foundEmployees = [self.employees filteredArrayUsingPredicate:finalPredicate]; 
} 

=[c]確實不區分大小寫的比較。如果合適,您也可以使用BEGINSWITH[c]CONTAINS[c]

謂詞使用鍵值編碼,所以fullName,functionName等應該是員工對象的屬性。

+0

我還沒有測試過。只要我測試它,我會讓你知道的。 – StackFlower

+0

@StackFlower:有沒有反饋? –

相關問題