如何创建NSPredicate来搜索日历中的事件?

时间:2011-07-11 11:49:38

标签: iphone nspredicate

我尝试使用以下谓词来搜索日历中的事件,但我得到以下内容 异常'NSInvalidArgumentException',原因:'谓词不是用EKCalendarStore方法创建的'


//[_sender objectAtIndex:0] is Name of the event and [_sender objectAtIndex:1] is location of the event

NSPredicate *prediction=[NSPredicate predicateWithFormat:@"(SELF contains[cd] %@)", [NSString stringWithFormat: @"%@%@",[_sender objectAtIndex:0],[_sender objectAtIndex:1]]];
NSArray*events=[eventStore eventsMatchingPredicate:prediction];

2 个答案:

答案 0 :(得分:0)

我按照EventKit编程指南获得了解决方案。

   NSPredicate *prediction=[eventStore predicateForEventsWithStartDate:statedate endDate:enddate calendars:[eventStore calendars]];
   NSArray*events=[eventStore eventsMatchingPredicate:prediction];

答案 1 :(得分:0)

这是 Swift 4.0 实施:

import EventKit

let eventStore = EKEventStore()
let datePredicate = eventStore.predicateForEvents(withStart: begin, 
    end: end,
    calendars: eventStore.calendars(for: .event))
let events = eventStore.events(matching: datePredicate)