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

Iphone 如何创建NSPredicate以搜索日历中的事件?,iphone,nspredicate,Iphone,Nspredicate,我试图使用下面的谓词来搜索日历中的事件,但得到了以下结果 异常“NSInvalidArgumentException”,原因:“谓词不是使用EKCalendarStore方法创建的” //[_sender objectAtIndex:0] is Name of the event and [_sender objectAtIndex:1] is location of the event NSPredicate *prediction=[NSPredicate predicateWithFor

我试图使用下面的谓词来搜索日历中的事件,但得到了以下结果 异常“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]; //[\u sender objectAtIndex:0]是事件的名称,[\u sender objectAtIndex:1]是事件的位置 NSPredicate*prediction=[NSPredicate predicateWithFormat:@“(自身包含[cd]]@)”,[NSString stringWithFormat:@“%@%@”,[[发送方对象索引:0],[发送方对象索引:1]]; NSArray*事件=[eventStore eventsMatchingPredicate:prediction];
我遵循EventKit编程指南,得到了解决方案

NSPredicate *prediction=[eventStore predicateForEventsWithStartDate:statedate endDate:enddate calendars:[eventStore calendars]]; NSArray*events=[eventStore eventsMatchingPredicate:prediction]; NSPredicate*prediction=[eventStore predicateForEventsWithStartDate:statedate endDate:endDate日历:[eventStore日历]]; NSArray*事件=[eventStore eventsMatchingPredicate:prediction];
以下是一个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)