PDA

View Full Version : NSDate keeps showing 2/12/1982 despite my efforts to override this




elorc
Apr 18, 2009, 11:46 PM
I've noticed this with a few projects actually. The first was a small project I was writing from scratch that incorporated a datepicker. I noticed that no matter what I tried to do, it would always show as 2/12/1982. I'd try to override it by using something like this:

NSDate *currentDate = [NSDate date];
[myDatePickerObj setValue:currentDate];

In place of setValue: I've also tried setDateValue: with the same results. No luck.

Also, in the Hillegass book's chapter 11 exercise, I saw the same behavior. In this particular example, he has the reader create a new class to override NSArrayController's newObject method.

CarArrayController.h
#import <Cocoa/Cocoa.h>

@interface CarArrayController : NSArrayController {
}

@end

CarArrayController.m
#import "CarArrayController.h"

@implementation CarArrayController

- (id)newObject
{
id newObj = [super newObject];
NSDate *now = [NSDate date];
[newObj setValue:now forKey:@"datePurchased"];

return newObj;
}

@end

When I run the application and create a new record, the datepicker still shows as 2/12/1982. Any ideas?



elorc
Apr 18, 2009, 11:52 PM
Ok I fixed the problem in the Hillegass example. I screwed up and didn't have the CanArrayController class set to the array controller I was using. I still can't figure out why the other code doesn't work. I'm putting it in my init override:

- (id)init
{
self = [super init];
userlist = [[NSMutableArray alloc] init];

// Set the date to today
NSDate *newDateVal = [NSDate date];
[myDatePicker setDateValue:newDateVal];
NSLog(@"The current date is %@", newDateVal);

return self;
}

dantherevelator
Apr 19, 2009, 12:15 AM
What happens if you comment out the message to myDatePicker?

elorc
Apr 19, 2009, 12:53 PM
What happens if you comment out the message to myDatePicker?

If I comment it out, I get a warning that says newDateVal is an unused variable. The debug console shows my NSLog line: "The current date is 2009-04-19 12:50:54 -0400"

lucasgladding
Apr 19, 2009, 03:04 PM
Try obtaining the dateValue from the NSDatePicker instance. I don't have Cocoa Programming in front of me, but I would guess that your outlet isn't set. When I have problems with setter methods, I usually try logging the result of the getter method first. That will tell you if the NIB has been loaded at that time and whether the connection has been made.

JoshDC
Apr 19, 2009, 05:40 PM
I'm fairly sure your problem is you're doing these initializations in the wrong place. If you're doing it how I think you are, when the init method is called datePicker isn't yet initialized itself, so the setValue: method is send to a null object. You can set a breakpoint and hover over datePicker to confirm this (it should be 0x0). Put interface initializations in the -(void)awakeFromNib method instead.