2013-01-02 17 views
1

首次構建NSPredicate使用OR錯誤的NSPredicate

我想用這個邏輯來搜索managedobjectcontext

Search for a, grab all matches 
Search for b, grab all matches, etc.... 

Nsarray *results = (has all a results, b results, etc); 

我試圖謂語是:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name== %@ OR name == %@ OR name == %@",a,b,c]; 

但是我得到的錯誤與此predicate ......

編輯:我寫的樣品方法

-(NSPredicate*)parsePartsIntoAPredicate:(NSMutableArray*)inputPartsNames{ 
    NSSet *keys=[NSSet setWithArray:inputPartsNames]; 
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"any [email protected] in %@", keys]; 
    NSLog(@"predicate = %@",predicate); 
    return predicate; 
} 

澄清:我有一個汽車數據庫(20,000)每輛車有多個部分。我想找到所有有a部分的汽車,以及所有有部件b的汽車,以及所有有c部分的汽車。然後,我想返回與部分a,b,c等汽車陣列...

如果你認爲有更好的方式讓我知道,但我接近這個倒退。我是說

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cars" inManagedObjectContext:[self managedObjectContext]]; 
    [fetchRequest setEntity:entity]; 
    [fetchRequest setPredicate:[self parsePartsIntoAPredicate:inputParts]]; 

    NSError *error; 
    NSArray *records = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error]; 

我在做什麼錯?

+2

你什麼錯誤?可能是錯字,但「NS預測」中的「p」應該是資本。 –

+1

儘量讓它更清楚,幫助沒有太多的信息是有點冒險! –

+0

要獲得有用的答案,您至少必須展示:1)託管對象實體的定義,2)確切的謂詞和獲取請求(複製/粘貼,無錯別字!),3)確切的錯誤消息。 –

回答

0

獲取所有Cars對象有name這是在keys集合或數組的字符串之一,使用

[NSPredicate predicateWithFormat:@"name IN %@", keys] 
+0

@downvoter:這裏有什麼不對嗎? –

2
NSString *key; 
NSMutableArray *tempArray; 
NSPredicate *searchForName = [NSPredicate predicateWithFormat:@"name = %@", key]; 
NSArray *filterArray = [tempArray filteredArrayUsingPredicate:searchForName]; 
NSLog(@"%@",filterArray); 

關鍵是你searchKeyword,tempArray是你CompleteArray其中的數據是否存在。

使用像這樣。請把你的數據。

+0

對現有數組進行排序或將謂詞放入提取請求會更快嗎?所以,從本質上講,我會問所有的汽車coredata,然後一個一個找到匹配每個部分的汽車? –

2

使用此

NSPredicate* predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ '%@'", @"SELF contains[c] ",searchText]]; 
+0

輸入必須是可變的...它可能是2個變量,可能是100.也會這樣做嗎?所以找到所有有,找到所有有B,找到所有有C,等... –