Re: Setting an NSDate with natural language string
Re: Setting an NSDate with natural language string
- Subject: Re: Setting an NSDate with natural language string
- From: Nicholas Riley <email@hidden>
- Date: Sat, 19 Oct 2002 09:48:12 -0500
- Mail-followup-to: Rick Anderson <email@hidden>, Cocoa Dev Mailing List <email@hidden>
On Sat, Oct 19, 2002 at 05:08:21AM -0700, Rick Anderson wrote:
>
In the interface, I have two text fields (dateBox and timebox). This
>
chunk of code is attempting to pull out the date and time in those
>
fields and set an NSDate object based on those values.
>
>
Could someone shed some light on what I'm overlooking here?
>
Furthermore, if anyone has ideas on how to approach this, I'm open to
>
all suggestions.
>
>
NSMutableString *theDateStr = [[NSMutableString alloc] init];
>
>
[theDateStr appendString:[dateBox stringValue]];
>
[theDateStr appendString:@" "];
>
[theDateStr appendString:[timeBox stringValue]];
Unless you're sure of the format of dateBox and timeBox, I wouldn't do
this. You can very easily get a total mess in that string.
>
NSDate *theDate = [[NSDate alloc] dateWithNaturalLanguageString:theDateStr];
>
>
I'm stumped. Why would the code compile if NSDate doesn't recognize
>
dateWithNaturalLanguageString:? I know it's a class method (I
>
understand the distinction between a class and an instance method), but
>
is that not the context in which I'm using it here?
No, it's not. Class methods are invoked by using the class itself as
the receiver, i.e. [NSDate dateWithNaturalLanguageString: ...].
There are quite a few bugs in NSDate/NSCalendarDate and natural
language formatting. My Pester application
<
http://web.sabi.net/nriley/software/#pester> does what you describe -
it has separate fields for date and time and makes a NSDate out of
them. It's not perfect - it behaves weirdly if you enter a date in
the time field, for example, but it works reasonably well with this
code:
- (void)setForDate:(NSDate *)date atTime:(NSDate *)time;
{
NSCalendarDate *calTime, *calDate;
[...]
// XXX if calTime's date is different from the default date, complain
calTime = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
[time time IntervalSinceReferenceDate]];
calDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
[date timeIntervalSinceReferenceDate]];
[...]
[self setForDateAtTime:
[[[NSCalendarDate alloc] initWithYear: [calDate yearOfCommonEra]
month: [calDate monthOfYear]
day: [calDate dayOfMonth]
hour: [calTime hourOfDay]
minute: [calTime minuteOfHour]
second: [calTime secondOfMinute]
timeZone: nil] autorelease]];
}
I've removed some error checking - see PSAlarmSetController.m and
PSAlarm.m for the complete code. I haven't had a chance to file the
bugs yet, but there are comments at the top of PSAlarmSetController.m
which should summarize the problems I ran into.
At some point I plan to rewrite this stuff to use Carbon as it seems
its date formatting is far less flaky. iCal, while written in Cocoa,
includes some Carbon-like date controls which I really wish Apple
would make public; it's so annoying trying to implement separate date
and time fields.
--
=Nicholas Riley <email@hidden> | <
http://www.uiuc.edu/ph/www/njriley>
Pablo Research Group, Department of Computer Science and
Medical Scholars Program, University of Illinois at Urbana-Champaign
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.