2013-06-20 63 views
0

進出口使用coredata在我的應用程序,IOS核心數據謂詞搜索類似的詞

我可以搜索所有實體的首個字母,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name MATCHES '^[hH].*'"]; 
//sort descriptor! 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; 
NSArray *companyProducts = [Company allWithPredicate:predicate sortDescriptors:@[sortDescriptor]]; 
Company *theCompany; 
NSLog(@"tus companies number:: %d", companyProducts.count); 
for (theCompany in companyProducts) { 
    NSLog(@"tus companies:: %@", theCompany.name); 
} 

所以在這種情況下,我會得到所有的公司都開始小時無論大寫還是小寫...

但是如果我需要搜索多個字母的匹配嗎?無論是大寫或小寫,

例如尋找:

超 曉星

所以,我需要知道如何構建我正則表達式?在我的房產上搜索n個字符?

謝謝!

+0

您可以BEGINSWITH – Anupdas

+0

嘗試喜@Anupdas我NSPredicate *謂詞=試圖[NSPredicate predicateWithFormat:@ 「名BEGINSWITH 'H'」]; 但問題是如何使它與小寫或較低和較高的組合?謝謝! – MaKo

回答

4

嘗試用

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH [c] %@",@"H"]; 
+0

嗨,感謝偉大的工作!,將允許時接受!,但另一個問題,這是正則表達式?所以我學會了如何構造這個謂詞? TNX! – MaKo

+0

@MaKo對不起,我不確定他們是如何實現這一目標的。但是,您可以參考此[鏈接](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html)以獲取NSPredicate中的更多可用選項 – Anupdas