Categories Challenge


#1

Here is my solution to the challenge. :3

header file

#import <Foundation/Foundation.h>

@interface NSDate (CMR_dateConvenience)

-(NSDate *)dateConvenience:(int)year month:(int)month day:(int)day;

@end

implementation file

#import "NSDate+CMR_dateConvenience.h"

@implementation NSDate (CMR_dateConvenience)

- (NSDate *)dateConvenience:(int)year month:(int)month day:(int)day
{
    // create a pointer to a date components object
    NSDateComponents *newDate = [[NSDateComponents alloc]init];
    [newDate setYear:year];
    [newDate setMonth:month];
    [newDate setDay:day];
    
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDate *updatedNewDate = [gregorian dateFromComponents:newDate];
    
    return updatedNewDate;
}

@end

main file


#import <Foundation/Foundation.h>
#import "NSDate+CMR_dateConvenience.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        NSDate *myDate = [[NSDate alloc]init];
        NSLog(@"my date is %@", [myDate dateConvenience:2014 month:9 day:29]);
        
    }
    return 0;
}