我在我的应用程序中使用MBCalendarKit,我可以创建这样的事件:
CKCalendarEvent* aCKCalendarEvent = [[CKCalendarEvent alloc] init];
[_infoDict setObject:@"Joint Meetings with KY State Horticulture Society, KY Vegetable Growers Association, KY Vineyard Society, and KY Farmer Market Association \nEmbassy Suites\nLexington, KY" forKey:@"Jan Joint"];
aCKCalendarEvent.date = [dateformatter dateFromString: @"01/06/2014"];
aCKCalendarEvent.title = @"Joint Meetings";
aCKCalendarEvent.info = [_infoDict objectForKey:@"Jan Joint"];
[eventsArray addObject: aCKCalendarEvent];
[_eventsDict setObject: eventsArray forKey: [NSDate dateWithDay:06 month:01 year:2014]];
我可以在同一天创建另一个事件:
[_infoDict setObject:@"Washington, D.C." forKey:@"Congress Convenes"];
aCKCalendarEvent = [[CKCalendarEvent alloc] init];
aCKCalendarEvent.date = [dateformatter dateFromString: @"01/06/2014"];
aCKCalendarEvent.title = @"113th Congress Convenes";
[eventsArray addObject: aCKCalendarEvent];
[_eventsDict setObject: eventsArray forKey: [NSDate dateWithDay:06 month:01 year:2014]];
然后我可以在不同的日子创建另一个事件,如下所示:
eventsArray = [[NSMutableArray alloc] init];
[_infoDict setObject:@"Hilton Hotel \nMemphis, TN" forKey:@"Forage & Grassland"];
aCKCalendarEvent = [[CKCalendarEvent alloc] init];
aCKCalendarEvent.date = [dateformatter dateFromString: @"01/12/2014"];
aCKCalendarEvent.title = @"American Forage & Grassland Council Annual Conference";
[eventsArray addObject: aCKCalendarEvent];
[_eventsDict setObject: eventsArray forKey: [NSDate dateWithDay:12 month:01 year:2014]];
我正在尝试遍历plist文件,该文件包含创建所有事件的所有事件。我可以这样做:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Events" ofType:@"plist"];
NSDictionary *dictPri = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
NSMutableArray *arrEvents = [[NSMutableArray alloc] initWithArray:[dictPri objectForKey:@"List"]];
// Loop through each item in List array of Events.plist
for (NSDictionary *dict in arrEvents)
{
NSString *titleString = nil;
NSString *date = nil;
titleString = [NSString stringWithFormat:@"%@",[dict objectForKey:@"Title"]];
date = [NSString stringWithFormat:@"%@",[dict objectForKey:@"Date"]];
// NSDate *dateVariable = [dateformatter dateFromString:date];
// Create events
eventsArray = [[NSMutableArray alloc] init];
aCKCalendarEvent = [[CKCalendarEvent alloc] init];
aCKCalendarEvent.title = titleString;
aCKCalendarEvent.date = [dateformatter dateFromString: date];
[eventsArray addObject: aCKCalendarEvent];
[_eventsDict setObject:eventsArray forKey:aCKCalendarEvent.date];
}
问题是,如果我在同一天有多个事件,则只显示最新事件。我可以取出eventsArray = [[NSMutableArray alloc] init];
行,但新事件不仅会出现在其他事件的正确日期,而且会出现在其他日子。所以我想我可以抛出这样的东西来检查我是否需要该行,然后将事件添加到eventsDict,但我在-[CKCalendarEvent isEqualToDate:]: unrecognized selector sent to instance 0xc6bb880
上得到if ([[eventsArray objectAtIndex:i]isEqualToDate:aCKCalendarEvent.date])
:
BOOL dateExists;
for (int i = 0; i < [eventsArray count]; i++)
{
if ([[eventsArray objectAtIndex:i]isEqualToDate:aCKCalendarEvent.date])
{
// Array already contains object with this date
dateExists = YES;
}
else
{
dateExists = NO;
}
NSLog(@"%c", dateExists);
}
if (dateExists == YES)
{
[eventsArray addObject:aCKCalendarEvent];
[_eventsDict setObject:eventsArray forKey:aCKCalendarEvent.date];
}
else
{
eventsArray = [[NSMutableArray alloc]init];
[eventsArray addObject:aCKCalendarEvent];
[_eventsDict setObject:eventsArray forKey:aCKCalendarEvent.date];
}
所以基本上,我需要一种方法来遍历plist并确定程序是否需要在每个新事件上初始化eventsArray。
答案 0 :(得分:4)
您正在将事件与日期进行比较。请参阅下面的比较事件日期的代码。
if ([[(CKCalendarEvent *)[eventsArray objectAtIndex:i] date]isEqualToDate:aCKCalendarEvent.date])