使用NSPredicate确定字符串是否等于另一个字符串

Gar*_*tet 35 objective-c nsstring nsarray nspredicate

我有一个NSArray使用该[CalCalendarStore eventPredicateWithStartDate]方法返回的CalEvents .从返回的事件中,我试图只保留事件标题== @"on call"(不区分大小写)的那些事件.

我能够在数组中保留标题包含 @"on call"以下代码的事件(其中'events'是'NSArray'填充的CalEvents):

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

我尝试使用谓词格式字符串,如:

@"SELF.title == 'on call'" 但这似乎不起作用.

有更简单的方法吗?

Cor*_*ger 106

尝试 [NSPredicate predicateWithFormat:@"title ==[c] 'on call'"];

([c]使得相等比较不区分大小写.)

  • +1,虽然你可以在`==`之后抛出`[c]`修饰符,使它不区分大小写. (11认同)

Vla*_*mir 10

尝试使用格式谓词@"self.title like[c] 'on call'".以下示例代码输出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"
Run Code Online (Sandbox Code Playgroud)

  • 使用`=='和`like`进行字符串比较是否有区别? (3认同)