NSDate keeps showing 2/12/1982 despite my efforts to override this

Discussion in 'Mac Programming' started by elorc, Apr 18, 2009.

  1. elorc macrumors newbie

    Apr 1, 2009
    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.

    #import <Cocoa/Cocoa.h>
    @interface CarArrayController : NSArrayController {
    #import "CarArrayController.h"
    @implementation CarArrayController
    - (id)newObject
    	id newObj = [super newObject];
    	NSDate *now = [NSDate date];
    	[newObj setValue:now forKey:@"datePurchased"];
    	return newObj;
    When I run the application and create a new record, the datepicker still shows as 2/12/1982. Any ideas?
  2. elorc thread starter macrumors newbie

    Apr 1, 2009
    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;
  3. dantherevelator macrumors regular

    Oct 8, 2007
    What happens if you comment out the message to myDatePicker?
  4. elorc thread starter macrumors newbie

    Apr 1, 2009
    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"
  5. lucasgladding macrumors 6502

    Feb 16, 2007
    Waterloo, Ontario
    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.
  6. JoshDC macrumors regular

    Apr 8, 2009
    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.

Share This Page