2010-10-05 13 views

risposta

20

Ho scritto una categoria per questa attività. NSDate manca molti metodi utili.

@interface NSDate (missingFunctions) 
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day; 
@end 


@implementation NSDate (missingFunctions) 

+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day { 
    NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
    NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; 
    [components setYear:year]; 
    [components setMonth:month]; 
    [components setDay:day]; 
    return [calendar dateFromComponents:components]; 
} 
@end 
+0

Funziona come un fascino. Grazie :). – Carlo

+7

Non manca. Non è intenzionalmente lì. La teoria è che un ipotetico '+ [NSDate dateWithYear: month: day:]' farebbe presupporre agli sviluppatori che tutti usassero il calendario gregoriano. Meglio, nella mente di Apple, dividere la manipolazione moment-in-time ('NSDate') dalla manipolazione del calendario del mondo reale (' NSCalendar' e 'NSDateComponents'.) –

+0

thx per la spiegazione, non ci stava mai pensando in quel modo. –

Problemi correlati