Skip to main content

NSDateFormatter"s init method is deprecated?



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 ?




Comments

  1. 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.

    ReplyDelete
  2. 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!

    ReplyDelete
  3. 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?

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

    ReplyDelete
  5. 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.

    ReplyDelete
  6. 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.

    ReplyDelete
  7. 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.