0
我正在制作一个应用程序,用于创建新日历,然后使用该日历添加事件等。 我可以创建日历很好,但我试图运行检查以查看是否日历存在,所以,不要每次都创建一个同名的第二个日历。换句话说,只能创建新的日历ONCE。isEqualToString无法正常工作
我设置了一个int变量并运行一个循环来检查设备上每个日历的title属性,但是int变量永远不会被更改,即使我正在搜索匹配的日历名称的字符串。
下面是我对“检查日历”代码:
-(void)checkForCalendar {
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSArray *calendarArray = [eventStore calendarsForEntityType:EKEntityTypeEvent];
//NSLog(@"%@", calendarArray);
for (int x = 0; x < [calendarArray count]; x++) {
EKCalendar *cal = [calendarArray objectAtIndex:x];
NSString *title = [cal title];
if ([title isEqualToString:@"AFTP"]) {
calendarExists = 1;
}else{
calendarExists = 0;
}
}
[self createCalendar];
}
这里就是我对“创造”日历部分:(工作正常,我只是总是得到一个“ 。0" 代替1 calendarExists
INT)
-(void)createCalendar {
NSLog(@"%d",calendarExists);
if (calendarExists == 0) {
EKEventStore* eventStore = [[EKEventStore alloc] init];
NSString* calendarName = @"AFTP";
EKCalendar* calendar;
// Get the calendar source
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
if (source.sourceType == EKSourceTypeCalDAV)
{
localSource = source;
break;
}
}
if (!localSource)
return;
calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
calendar.source = localSource;
calendar.title = calendarName;
[eventStore saveCalendar:calendar commit:YES error:nil];
}
}
感谢darren,那肯定是问题所在。非常感谢。 – jhilgert00