• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSDateFormatter -initWithDateFormat:: vs. -init
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSDateFormatter -initWithDateFormat:: vs. -init


  • Subject: NSDateFormatter -initWithDateFormat:: vs. -init
  • From: Jerry Krinock <email@hidden>
  • Date: Fri, 11 Nov 2011 17:36:19 -0800

NSDateFormatter documentation indicates that -initWithDateFormat:allowNaturalLanguageString: is the recommended initializer for new designs, and furthermore that -init is not "available" after 10.5.

However, I find kind of the opposite: -initWithDateFormat:allowNaturalLanguageString gives unexpected results, but -init works perfectly.  I get the same results, building with either 10.6 or 10.7 SDK.  I'm running in 10.7.

--- Code --------

NSLog(@"behavior = %ld", [NSDateFormatter defaultFormatterBehavior]) ;

NSString* formatString = @"yyyy.MM.dd  'at' HH:mm:ss.SSS zzz" ;

NSDateFormatter* oldFormatter ;
oldFormatter = [[NSDateFormatter alloc] init] ;
[oldFormatter setDateFormat:formatString] ;

NSDateFormatter* newFormatter ;
newFormatter = [[NSDateFormatter alloc] initWithDateFormat:formatString
                                      allowNaturalLanguage:NO] ;
// This makes it worse:
// [newFormatter setFormatterBehavior:NSDateFormatterBehavior10_4] ;

NSDate* date = [NSDate date] ;
NSString* dateString ;

dateString = [oldFormatter stringFromDate:date] ;
NSLog(@"old: %@", dateString);

dateString = [newFormatter stringFromDate:date] ;
NSLog(@"new: %@", dateString);

[oldFormatter release] ;
[newFormatter release] ;

---- Result ----

behavior = 1040
old: 2011.11.11  at 17:20:59.493 PST
new: yyyy.MM.dd  'at' HH:mm:ss.SSS

----------------

Behavior 1040 is NSDateFormatterBehavior10_4, which is the newest behavior available.
As you can see, the old method works as expected, but the new method unexpectedly returns its format string.  If I uncomment the line after "This makes it worse", then instead of the format string I get an empty string.

What is going on, please?

Bonus question:  How can -init be not "available"?  Does this just mean that it defaults to the super -[NSObject init] ?

Thanks,

Jerry Krinock

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: NSDateFormatter -initWithDateFormat:: vs. -init
      • From: Greg Parker <email@hidden>
    • Re: NSDateFormatter -initWithDateFormat:: vs. -init
      • From: Dave DeLong <email@hidden>
  • Prev by Date: Re: Allocating too much memory kills my App rather than returning NULL
  • Next by Date: Re: 2 itunesconnect questions
  • Previous by thread: Re: How does LaunchServices decide which version of an app to open?
  • Next by thread: Re: NSDateFormatter -initWithDateFormat:: vs. -init
  • Index(es):
    • Date
    • Thread