16

Per

http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

The init method of NSDateFormatter is "Available in iPhone OS 2.0 through iPhone OS 3.2", and therefore not in 4.0. Now, it certainly works, but this seems odd. Is this is a mistake or is there some other way to create a NSDateFormatter?

4
  • Where did you read this?
    – prgmast3r
    Dec 14, 2010 at 15:49
  • Official documentantion: developer.apple.com/library/ios/#documentation/Cocoa/Reference/…
    – bontoJR
    Dec 14, 2010 at 16:04
  • I am pretty sure "setLocale" and "setTimeStyle" are the right ways to do this, I am just curious how you are supposed to get the NSDateFormatter instance now?
    – fuzzygoat
    Dec 14, 2010 at 16:08
  • +1, this is hilarious. I wonder what Apple is thinking? I have to think it's somehow a mistake. I hope someone figures this out. Dec 14, 2010 at 16:23

7 Answers 7

18

This deprecation is just apple cleaning up the headers for NSDateFormatter. init is already declared in NSObject which NSNumberFormatter inherits from. Redeclaring is not necessary, however in the implementation apple will override init as subclassess should provide implementations for the default initializer of the superclass.

2
  • Thank you very much for that explanation. VERY misleading on Apple's part. It should not be marked as deprecated.
    – dmkash
    Mar 6, 2011 at 22:12
  • However, it is still useful to have documentation for the init method for this class, to describe what the implementation in this class does. e.g. "initializes formatter with default format", etc.
    – user102008
    Apr 26, 2011 at 0:06
6

Like falconcreek said here, Apple is just clearing the docs.

That means that originally, - (id) init has been redeclared in the NSDateFormatter header file. That was unnecessary and somebody just removed it later. The documentation is probably generated automatically and didn't picked up that it was only the redecleration that was deprecated.

In short, use init as you wish with NSDateFormatter!

1
  • 1
    I think the documwentation is highly misleading. I've used the "no not helpful" link to send a comment to Apple.
    – JeremyP
    Dec 15, 2010 at 9:30
2

I would trust the header files over the documentation.

For example, Formatter Behaviors and OS Versions seems to contradict itself:

By default, on Mac OS X v10.4 instances of NSDateFormatter have the same behavior as they did on Mac OS X versions 10.0 to 10.3. On Mac OS X v10.5 and later, NSDateFormatter defaults to the 10.4+ behavior.

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.

It sounds like initWithDateFormat:allowNaturalLanguage: is actually the deprecated method?

1
  • Exactly right, it does seem that initWithDateFormat:allowNaturalLanguage: is actually the deprecated method. The header files are what one should refer to in circumstances like this.
    – paulbailey
    Dec 14, 2010 at 22:07
1

That’s really strange. Maybe +[NSDateFormatter new] will do the job?

1
  • 3
    If init is deprecated, then +new is, too, because +new is nothing but +alloc/-init anyway (but good guess).
    – bbum
    Dec 14, 2010 at 17:59
0

I use the init method in an multiple iOS4 apps and they are approved and in the store. I can think of no way to get it to work so it would seem like its just a typo on the docs.

0

The example code in the class reference still uses the init method, so I'd say its safe to use it in your own code.

0

You can proof yourself that NSDateFormatter has a different behaviour dependent on the initializer you choose and the SDK you are compiling with. The following is tested with SDK 10.5 and 10.6.

NSDateFormatter* df = [[NSDateFormatter alloc] initWithDateFormat:@"yyyyMMdd" allowNaturalLanguage:NO];
if ([df formatterBehavior] == NSDateFormatterBehavior10_0)
    NSLog(@"NSDateFormatterBehavior10_0");
else if ([df formatterBehavior] == NSDateFormatterBehavior10_4)
    NSLog(@"NSDateFormatterBehavior10_4");

This returns NSDateFormatterBehavior10_0.
Instead, when you use [[NSDateFormatter alloc] init]; the behaviour is NSDateFormatterBehavior10_4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.