我在 IOS 7 中的日期格式有问题我编写了以下代码:
// eventstart = "12.NOV.2013 13:45" is a NSString
// eventend = "13.NOV.2013 14:12" is a NSString
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd.MMM.yyyy HH:mm:ss"];
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:@"dd.MMM.yyyy HH:mm:ss"];
[dateFormat setTimeZone:gmt];
[dateFormat2 setTimeZone:gmt];
NSDate *date = [dateFormat2 dateFromString:eventstart];
NSDate *date2 = [dateFormat dateFromString:eventend];
现在我将事件设置为:
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{ }
else if (!granted)
{ }
else
{
event.startDate = date;
event.endDate = date2;
event.calendar = [eventStore defaultCalendarForNewEvents];
NSString *title = [NSString stringWithFormat:@"%@",_type_s];
event.title = title;
NSString *location = [NSString stringWithFormat:@"%@-%@",_from_s,_to_s];
event.location = location;
NSString *notes = [NSString stringWithFormat:@"in GMT: \n %@ \n to \n %@",_starttime_s,_endtime_s];
event.notes = notes;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}
});
}];
问题是它适用于模拟器,但不适用于 Iphone 本身!我想我的日期格式有问题,因为调试器中的 date or date2 总是 nill .....
有人可以帮我解决这个问题或给我解决问题的提示吗!
最好的问候格特