开发者

Cannot program start date in EKEvent

I have used the following code to get a start date for my new EKEvent:

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

The text is from a textbox and for example purposes, I put "2011/09/16".

It came up with this error:

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   lib开发者_Python百科objc.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) 

Why is this?


EKEvent's startDate property is declared as an NSDate but you are passing it an NSString. You need to parse the string into a NSDate first and then set the event date:

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


@Tim Dean was on the right track, but that initializer is only used to return OS 10.0 style date formatters and isn't even supported on iOS.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜