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

发布于 2024-09-04 02:24:32 字数 571 浏览 5 评论 0原文

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

我可以使用以下代码将标题包括 @"on call" 的事件保留在数组中(其中“events”是填充有 CalEvents):

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

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

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

有没有更简单的方法来做到这一点?

I have an NSArray of CalEvents returned with the [CalCalendarStore eventPredicateWithStartDate] method. From the events returned, I am trying to keep only those in which the title of the event == @"on call" (case-insensitive).

I am able to keep in the array those events whose title includes @"on call" with the following code (where 'events' is a 'NSArray' populated with CalEvents):

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

I've tried using a predicate format string like:

@"SELF.title == 'on call'" but this doesn't seem to work.

Is there an easier way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫多说 2024-09-11 02:24:32

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

[c] 使相等比较不区分大小写。)

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

(The [c] makes the equality comparison case-insensitive.)

妖妓 2024-09-11 02:24:32

尝试使用格式 @"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"

Try predicate with format @"self.title like[c] 'on call'". The following sample code outputs 2 strings:

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文