DateMonger Challenge using dateWithNaturalLanguageString


#1

I used NSDate dateWithNaturalLanguageString to solve the DateMonger challenge, and noticed that the other solutions posted did not. In the documentation it says that using this method may give unexpected results and is discouraged, but if we’re feeding in specific integers, is there a reason one shouldn’t use this method?

NSDate+DateConvenience.h

[code]#import <Foundation/Foundation.h>

@interface NSDate (DateConvenience)

  • (NSDate *)midnightExpress:(int)year month:(int)month day:(int)day;

@end[/code]

NSDate+DateConvenience.m

[code]#import “NSDate+DateConvenience.h”

@implementation NSDate (DateConvenience)

  • (NSDate *)midnightExpress:(int)year month:(int)month day:(int)day;
    {
    NSDate *date = [NSDate dateWithNaturalLanguageString:[NSString stringWithFormat:@“12am %d/%d/%d”, month, day, year]];
    return date;
    }

@end[/code]

main.m

[code]#import <Foundation/Foundation.h>
#import “NSDate+DateConvenience.h”

int main(int argc, const char * argv[]) {
@autoreleasepool {
int day = 9;
int month = 10;
int year = 14;

    NSDate *thisday = [NSDate midnightExpress:year month:month day:day];
    NSLog(@"The date given is: %@", thisday);
    
    
}
return 0;

}[/code]


#2

I can’t think of a reason either, at least not for this specific example. The only advantage I can see with using date components is you could derive a more detailed NSDate instance by feeding it more components but the Challenge only cares about YY MM DD.