2010-06-03 40 views
34

我有一個與[CalCalendarStore eventPredicateWithStartDate]方法一起返回的CalEvents的NSArray。從返回的事件中,我試圖只保留事件標題== @"on call"(不區分大小寫)的事件。使用NSPredicate來確定一個字符串是否等於另一個字符串

我能夠保持在陣列中,其標題包括@"on call"用下面的代碼(其中「事件」是「NSArray的」填充CalEvents)這些事件:

NSPredicate *onCallPredicate = [NSPredicate predicateWithFormat:@"(SELF.title CONTAINS[c] 'on call')"]; 
[events filteredArrayUsingPredicate:onCallPredicate]; 

我試着使用謂詞格式字符串如:

@"SELF.title == 'on call'"但這似乎並不奏效。

有沒有更簡單的方法來做到這一點?

回答

99

[NSPredicate predicateWithFormat:@"title ==[c] 'on call'"];嘗試

(該[c]使得等式比較不區分大小寫的。)與格式@"self.title like[c] 'on call'"

+11

+1,儘管您可以在'=='之後拋出'[c]'修飾符以使其不區分大小寫。 – 2010-06-03 15:36:56

+1

將其編輯爲區分大小寫。 – 2010-06-03 15:38:54

+0

只是好奇,當沒有任何(* ?.)通配符時MATCH [n]會爲==工作嗎? – Samuel 2016-08-25 05:10:29

10

嘗試謂詞。以下示例代碼輸出2個字符串:

NSArray* ar = [NSArray arrayWithObjects:@"on call", @"I'm on call", @"lala", @"On call", nil]; 
NSArray* filt = [ar filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self like[c] 'on call'"]]; 
NSLog([filt description]); 

//Output 
"on call", 
"On call" 
+3

在字符串比較中使用'=='和'like'有什麼區別嗎? – Garry 2010-06-03 17:33:25

+0

看起來就像你的情況一樣。但是如果你想在字符串比較中使用通配符,那麼'=='將不起作用,你需要使用LIKE來代替。 – Vladimir 2010-06-03 19:34:55

相關問題