简体   繁体   English

在EKReminder中设置startDateComponents时出现“无效的日期组件”错误

[英]“Invalid date component” error when set startDateComponents in an EKReminder

I want to save a reminder, but when I set the StartDateComponents, it always says Invalid date component. 我想保存一个提醒,但是当我设置StartDateComponents时,它总是显示无效的日期组件。 I try several ways(you can see my commented code) to set the value, but failed. 我尝试了几种方法(您可以看到我的注释代码)来设置值,但是失败了。

EKReminder *reminder = [EKReminder reminderWithEventStore:appDelete.eventStore];

        NSCalendar *gregorian = [NSCalendar currentCalendar];//[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

        reminder.title = @"title";
        NSString *timeStr = @"20121115070800";

        NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
        [inputFormatter setCalendar:gregorian];
        [inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
        [inputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        NSDate *parseDate = [inputFormatter dateFromString:timeStr];

        NSDateComponents *dateComponents = [gregorian components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                       fromDate:parseDate];
        NSDateComponents *timeComponents = [gregorian components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                       fromDate:parseDate];
        NSDateComponents *dateComps = [[NSDateComponents alloc] init];
        [dateComps setDay:[dateComponents day]];
        [dateComps setMonth:[dateComponents month]];
        [dateComps setYear:[dateComponents year]];
        [dateComps setHour:[timeComponents hour]];
        // Notification will fire in one minute
        [dateComps setMinute:[timeComponents minute]];
        [dateComps setSecond:[timeComponents second]];
        [dateComps setTimeZone:[NSTimeZone localTimeZone]];

        reminder.calendar = /*[NSCalendar autoupdatingCurrentCalendar];*/[eventStore defaultCalendarForNewEvents];
        reminder.calendar = [EKCalendar calendarWithEventStore:eventStore];

        //NSDateComponents *comp = [[NSDateComponents alloc] init];
        /*comp.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        comp.year = [[timeStr substringToIndex:3] integerValue];
        comp.month = [[timeStr substringWithRange:NSMakeRange(4,2)] integerValue];
        comp.day = [[timeStr substringWithRange:NSMakeRange(6, 2)] integerValue];
        comp.hour = [[timeStr substringWithRange:NSMakeRange(8, 2)] integerValue];
        comp.minute = [[timeStr substringWithRange:NSMakeRange(10, 2)] integerValue];
        comp.second = [[timeStr substringWithRange:NSMakeRange(12, 2)] integerValue];*/

        //reminder.startDateComponents = [gregorian components:     (NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit/*|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit*/) fromDate:parseDate];

        [reminder setStartDateComponents:dateComps];// this line will throw the error:Invalid date component

It seemed the main issue was these 2 lines: 似乎主要问题是以下两行:

reminder.calendar = /*[NSCalendar autoupdatingCurrentCalendar];*/[eventStore defaultCalendarForNewEvents];
reminder.calendar = [EKCalendar calendarWithEventStore:eventStore];

Replace with this line: 替换为这一行:

reminder.calendar = [eventStore defaultCalendarForNewReminders];   

Here are the complete working code that also included a little extra for completeness: 这是完整的工作代码,其中还包括一些完整性方面的内容:

EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityMaskEvent completion:^(BOOL granted, NSError *error) {
    if (granted)
    {
        EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];

        NSCalendar *gregorian = [NSCalendar currentCalendar];//[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

        reminder.title = @"title44433333";
        NSString *timeStr = @"20121115070800";

        NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
        [inputFormatter setCalendar:gregorian];
        [inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
        [inputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        NSDate *parseDate = [inputFormatter dateFromString:timeStr];

        NSDateComponents *dateComponents = [gregorian components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                        fromDate:parseDate];
        NSDateComponents *timeComponents = [gregorian components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                        fromDate:parseDate];
        NSDateComponents *dateComps = [[NSDateComponents alloc] init];
        [dateComps setDay:[dateComponents day]];
        [dateComps setMonth:[dateComponents month]];
        [dateComps setYear:[dateComponents year]];
        [dateComps setHour:[timeComponents hour]];
        // Notification will fire in one minute
        [dateComps setMinute:[timeComponents minute]];
        [dateComps setSecond:[timeComponents second]];
        [dateComps setTimeZone:[NSTimeZone localTimeZone]];

        reminder.calendar = [eventStore defaultCalendarForNewReminders];   //here is the replacement

        [reminder setStartDateComponents:dateComps];// this line will throw the error:Invalid date component

        NSError *error = nil;
        [eventStore saveReminder:reminder commit:YES error:&error];
        if (error) NSLog(@"error = %@", error);
    }

}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS - Amazon S3 - 错误消息:当电话日期设置为某个过去日期时,“网络连接已丢失” - iOS - Amazon S3 - Error Message: “The network connection was lost” when phone date set to some past date 编译时出现“Invalid initializer”错误 - “Invalid initializer” error when compiling 请求令牌时收到错误无效签名 - got error invalid signature when requesting token 未包装可选类型EKReminder的值 - Value of optional type EKReminder not unwrapped 当日期来自不同时区时,在 IOS 中为 angularjs 项目返回无效日期 - Returning Invalid date in IOS for angularjs project when date is from different timezone 编译器错误:尝试更改UISwitch的来源时分配中的值无效 - Compiler Error: Invalid value in assignment when trying to change the origin of a UISwitch 通过UIWebview显示html时出现错误“无效的CFStringRef” - Error 'Invalid CFStringRef' when displaying html through UIWebview 在设备上录制时收到无效的锚定时间错误的ConvertInput - ConvertInput using invalid anchor time error received when recording on device Iphone 字符串中的无效日期 - Iphone invalid date from string 在UIActionSheet中添加UIPickerView时,iOS 7上的上下文错误无效? - Invalid context error on iOS 7 when adding a UIPickerView inside a UIActionSheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM