Re: NSDateFormatter -initWithDateFormat:: vs. -init
Re: NSDateFormatter -initWithDateFormat:: vs. -init
- Subject: Re: NSDateFormatter -initWithDateFormat:: vs. -init
- From: Dave DeLong <email@hidden>
- Date: Fri, 11 Nov 2011 17:40:51 -0800
I think you've misunderstood the documentation. -init is the preferred method for creating 10.4+ date formatters.
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/initWithDateFormat:allowNaturalLanguage:
Dave
Sent from Jane
On Nov 11, 2011, at 5:36 PM, Jerry Krinock <email@hidden> wrote:
>
> 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
_______________________________________________
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