以下是代码:
-(int)getSystemDay
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorian components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger day = [components weekday];
int weekday = (int) day;
return weekday;
}
据我所知,星期日的int总是1,但在我打印weekday
的情况下,它给出-1。为什么会这样?
答案 0 :(得分:2)
您没有指定NSCalendarUnitWeekday
组件,因此不会检索该值
Objective-C中未指定的日期组件为-1。
如果您只需要一个组件,则有一种更方便的方式:
-(NSInteger)getSystemDay
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
return [gregorian component:NSCalendarUnitWeekday fromDate:[NSDate date]];
}