8
Je cherche quelque chose comme:Initialiser NSDate avec date personnalisée
NSDate *date = [[NSDate alloc] initWithYear:1984 month:10 Day:8];
Est-il possible de faire quelque chose comme ça?
Merci!
Je cherche quelque chose comme:Initialiser NSDate avec date personnalisée
NSDate *date = [[NSDate alloc] initWithYear:1984 month:10 Day:8];
Est-il possible de faire quelque chose comme ça?
Merci!
J'ai écrit une catégorie pour cette tâche. NSDate manque beaucoup de méthodes utiles.
@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
Fonctionne comme un charme. Je vous remercie :). – Carlo
Il ne manque pas. C'est intentionnellement pas là. La théorie est qu'un hypothétique '+ [NSDate dateWithYear: mois: jour:]' ferait supposer aux développeurs que tout le monde a utilisé le calendrier grégorien. Mieux, dans l'esprit d'Apple, de séparer la manipulation momentanée ('NSDate') de la manipulation du calendrier réel ('NSCalendar' et' NSDateComponents'.) –
thx pour l'explication, n'a jamais pensé de cette façon. –