我怎样才能创建一个NSDate
从日、月、年开始?似乎没有任何方法可以做到这一点,他们已经删除了类方法dateWithString
(他们为什么要那样做?!)。
您可以为此编写一个类别。我这样做了,代码如下:
// NSDateCategory.h
#import <Foundation/Foundation.h>
@interface NSDate (MBDateCat)
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day;
@end
// NSDateCategory.m
#import "NSDateCategory.h"
@implementation NSDate (MBDateCat)
+ (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
像这样使用它:NSDate *aDate = [NSDate dateWithYear:2010 month:5 day:12];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)