无法在EKEvent中编程开始日期

时间:2011-09-15 19:54:56

标签: iphone objective-c ios xcode calendar

我使用以下代码来获取新EKEvent的开始日期:

event.startDate = [NSString stringWithFormat:@"%@", dateField.text];
event.endDate   = [[NSDate alloc] initWithTimeInterval:10 sinceDate:event.startDate];

文本来自文本框,例如,我把“2011/09/16”。

它出现了这个错误:

2011-09-15 20:53:20.541 iDHSB[205:707] -[NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance     
0x10ef30
2011-09-15 20:53:20.566 iDHSB[205:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-  
[NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x10ef30'
*** Call stack at first throw:
(
0   CoreFoundation                      0x3624c64f __exceptionPreprocess + 114
1   libobjc.A.dylib                     0x36f9fc5d objc_exception_throw + 24
2   CoreFoundation                      0x362501bf -[NSObject(NSObject) doesNotRecognizeSelector:] + 102
3   CoreFoundation                      0x3624f649 ___forwarding___ + 508
4   CoreFoundation                      0x361c6180 _CF_forwarding_prep_0 + 48
5   EventKit                            0x31b7bc4d -[EKEvent setStartDate:] + 168
6   iDHSB                               0x00007b8b -[iDHSB_MobileAppDelegate alertView:clickedButtonAtIndex:] + 690
7   UIKit                               0x32a63cf3 -[UIAlertView(Private) _buttonClicked:] + 230
8   CoreFoundation                      0x361bc571 -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
9   UIKit                               0x32955ec9 -[UIApplication sendAction:to:from:forEvent:] + 84
10  UIKit                               0x32955e69 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
11  UIKit                               0x32955e3b -[UIControl sendAction:to:forEvent:] + 38
12  UIKit                               0x32955b8d -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 356
13  UIKit                               0x32956423 -[UIControl touchesEnded:withEvent:] + 342
14  UIKit                               0x32954bf5 -[UIWindow _sendTouchesForEvent:] + 368
15  UIKit                               0x3295456f -[UIWindow sendEvent:] + 262
16  UIKit                               0x3293d313 -[UIApplication sendEvent:] + 298
17  UIKit                               0x3293cc53 _UIApplicationHandleEvent + 5090
18  GraphicsServices                    0x32813e77 PurpleEventCallback + 666
19  CoreFoundation                      0x36223a97 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
20  CoreFoundation                      0x3622583f __CFRunLoopDoSource1 + 166
21  CoreFoundation                      0x3622660d __CFRunLoopRun + 520
22  CoreFoundation                      0x361b6ec3 CFRunLoopRunSpecific + 230
23  CoreFoundation                      0x361b6dcb CFRunLoopRunInMode + 58
24  GraphicsServices                    0x3281341f GSEventRunModal + 114
25  GraphicsServices                    0x328134cb GSEventRun + 62
26  UIKit                               0x32967d69 -[UIApplication _run] + 404
27  UIKit                               0x32965807 UIApplicationMain + 670
28  iDHSB                               0x00002cfd main + 48
29  iDHSB                               0x00002cc8 start + 40
)
terminate called after throwing an instance of 'NSException'
(gdb) 

为什么会这样?

2 个答案:

答案 0 :(得分:3)

EKEvent的startDate属性被声明为NSDate,但您传递的是NSString。您需要先将字符串解析为NSDate,然后设置事件日期:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY/MM/dd"];
event.startDate = [formatter dateFromString:dateField.text];

答案 1 :(得分:1)

@Tim Dean在正确的轨道上,但该初始化程序仅用于返回OS 10.0样式的日期格式化程序,甚至在iOS上都不支持。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY/MM/dd"];
event.startDate = [dateFormatter dateFromString:dateField.text];