NSDateFormatter initialization+formatting not working

  • Hello,

      I'm not getting why this is not working:

    NSDate *date = [NSDate date];

    NSDateFormatter *df1 = [[NSDateFormatter alloc] initWithDateFormat:@"EEE d" allowNaturalLanguage:NO];
    NSString *s1 = [df1 stringFromDate:date];

    s1 ---> @"EEE d";

    NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
    [df2 setDateFormat:@"EEE d"];
    NSString *s2 = [df2 stringFromDate:date];

    s2 ---> @"Sat 19"  (on a Saturday the 19th)

    I could live with that but the documentation tells me that -init is deprecated in 10.6? Is that really true? When I look at the NSDateFormatter header things look quite differently:

    #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
    @interface NSDateFormatter (NSDateFormatterCompatibility)

    - (id)initWithDateFormat:(NSString *)format allowNaturalLanguage:(BOOL)flag;
    - (BOOL)allowsNaturalLanguage;

    @end
    #endif

    Which to me tries to convey that -initWithDateFormat: is deprecated -- not by saying so directly of course.

    So who is wrong, docs or header? And why doesn't the -initWithDateFormat: initializer work?

    Puzzled
    Markus
    --
    __________________________________________
    Markus Spoettl
  • On Feb 19, 2011, at 9:53 PM, Markus Spoettl wrote:

    > NSDateFormatter *df1 = [[NSDateFormatter alloc] initWithDateFormat:@"EEE d" allowNaturalLanguage:NO];
    > NSString *s1 = [df1 stringFromDate:date];
    >
    > s1 ---> @"EEE d";
    >
    > NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
    > [df2 setDateFormat:@"EEE d"];
    > NSString *s2 = [df2 stringFromDate:date];
    >
    > s2 ---> @"Sat 19"  (on a Saturday the 19th)

    > And why doesn't the -initWithDateFormat: initializer work?

    I think it does work, but produces a formatter using the old-style behavior.  From <http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Found
    ation/Classes/NSDateFormatter_Class/
    >:

    > If you initialize a formatter using initWithDateFormat:allowNaturalLanguage:, you are (for backwards compatibility reasons) creating an “old-style” date formatter. To use the new behavior, you initialize the formatter with init. If necessary, you can set the default class behavior using setDefaultFormatterBehavior:), you can set the behavior for an instance using setFormatterBehavior: message with the argument NSDateFormatterBehavior10_4.

    My best guess about -init is not so much that it is deprecated to invoke that method, but that there used to be an override with its own documentation, but now it's just implicit that -init is inherited from NSObject.  So, the old method documentation was removed from the class reference and put in a "deprecated" page.  If you look at the header, there is a commented-out declaration of -init with a comment saying it is the designated initializer.  In previous versions of that header, the -init method declaration was not commented out.

    Regards,
    Ken
  • On Feb 19, 2011, at 11:18 PM, Ken Thomases wrote:
    >> And why doesn't the -initWithDateFormat: initializer work?
    >
    > I think it does work, but produces a formatter using the old-style behavior.  From <http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Found
    ation/Classes/NSDateFormatter_Class/
    >:

    Thanks for pointing that out.

    > My best guess about -init is not so much that it is deprecated to invoke that method, but that there used to be an override with its own documentation, but now it's just implicit that -init is inherited from NSObject.  So, the old method documentation was removed from the class reference and put in a "deprecated" page.  If you look at the header, there is a commented-out declaration of -init with a comment saying it is the designated initializer.  In previous versions of that header, the -init method declaration was not commented out.

    This sounds right. Calling -init deprecated is most certainly a documentation bug in this case. Thanks for clearing this one up for me!

    Regards
    Markus
    --
    __________________________________________
    Markus Spoettl
previous month february 2011 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28            
Go to today