YYKit源码探究(五十三) —— NSDate分类之Component Properties(一)

版本记录

版本号 时间
V1.0 2018.03.30

前言

iOS圈内有几个人大家基本都知道,比如说王巍、唐巧,还有YYKit框架的作者现任职于滴滴的郭曜源 - ibireme等。这里有一篇唐巧对他的专访,还有他的 GitHub - Yaoyuan 和 博客,这里贴出来框架YYKit 框架。接下来几篇我们就一起来看一下这个框架。感兴趣的可以看上面写的几篇。
1. YYKit源码探究(一) —— 基本概览
2. YYKit源码探究(二) —— NSString分类之Hash(一)
3. YYKit源码探究(三) —— NSString分类之Encode and decode(二)
4. YYKit源码探究(四) —— NSString分类之Drawing(三)
5. YYKit源码探究(五) —— NSString分类之Regular Expression(四)
6. YYKit源码探究(六) —— NSString分类之NSNumber Compatible(五)
7. YYKit源码探究(七) —— NSString分类之Utilities(六)
8. YYKit源码探究(八) —— NSNumber分类(一)
9. YYKit源码探究(九) —— UIFont分类之架构分析和Font Traits(一)
10. YYKit源码探究(十) —— UIFont分类之Create font(二)
11. YYKit源码探究(十一) —— UIFont分类之Load and unload font(三)
12. YYKit源码探究(十二) —— UIFont分类之Dump font data(四)
13. YYKit源码探究(十三) —— UIImage分类之框架结构和Create image部分(一)
14. YYKit源码探究(十四) —— UIImage分类之Image Info(二)
15. YYKit源码探究(十五) —— UIImage分类之Modify Image(三)
16. YYKit源码探究(十六) —— UIImage分类之Image Effect(四)
17. YYKit源码探究(十七) —— UIImageView分类之架构和image部分(一)
18. YYKit源码探究(十八) —— UIImageView分类之highlight image部分(二)
19. YYKit源码探究(十九) —— UIScreen分类(一)
20. YYKit源码探究(二十) —— UIScrollView分类(一)
21. YYKit源码探究(二十一) —— UITableView分类(一)
22. YYKit源码探究(二十二) —— UITextField分类(一)
23. YYKit源码探究(二十三) —— UIView分类(一)
24. YYKit源码探究(二十四) —— UIPasteboard分类(一)
25. YYKit源码探究(二十五) —— UIGestureRecognizer分类(一)
26. YYKit源码探究(二十六) —— UIDevice分类框架及Device Information(一)
27. YYKit源码探究(二十七) —— UIDevice分类之Network Information(二)
28. YYKit源码探究(二十八) —— UIDevice分类之Disk Space(三)
29. YYKit源码探究(二十九) —— UIDevice分类之Memory Information(四)
30. YYKit源码探究(三十) —— UIDevice分类之CPU Information(五)
31. YYKit源码探究(三十一) —— UIControl分类(一)
32. YYKit源码探究(三十二) —— UIColor分类之Create a UIColor Object(一)
33. YYKit源码探究(三十三) —— UIColor分类之Get color's description(二)
34. YYKit源码探究(三十四) —— UIColor分类之Retrieving Color Information(三)
35. YYKit源码探究(三十五) —— UIButton分类之image(一)
36. YYKit源码探究(三十六) —— UIButton分类之background image(二)
37. YYKit源码探究(三十七) —— UIBezierPath分类(一)
38. YYKit源码探究(三十八) —— UIBarButtonItem分类(一)
39. YYKit源码探究(三十九) —— UIApplication分类(一)
40. YYKit源码探究(四十) —— NSTimer分类(一)
41. YYKit源码探究(四十一) —— NSParagraphStyle分类(一)
42. YYKit源码探究(四十二) —— NSObject分类之YYModel(一)
43. YYKit源码探究(四十三) —— NSObject分类之KVO(二)
44. YYKit源码探究(四十四) —— NSObject分类之Sending messages with variable parameters(三)
45. YYKit源码探究(四十五) —— NSObject分类之Swap method (Swizzling)(四)
46. YYKit源码探究(四十六) —— NSObject分类之Associate value(五)
47. YYKit源码探究(四十七) —— NSObject分类之Other(六)
48. YYKit源码探究(四十八) —— NSNotificationCenter分类(一)
49. YYKit源码探究(四十九) —— NSKeyedUnarchiver分类(一)
50. YYKit源码探究(五十) —— NSDictionary分类之Dictionary Convertor(一)
51. YYKit源码探究(五十一) —— NSDictionary分类之Dictionary Value Getter(二)
52. YYKit源码探究(五十二) —— NSDictionary分类之NSMutableDictionary(三)

回顾

上一篇主要介绍了NSMutableDictionary分类部分,这一篇主要看一下NSDate分类的Component Properties部分。


框架结构

下面我们看一下框架结构

YYKit源码探究(五十三) —— NSDate分类之Component Properties(一)_第1张图片

API - Component Properties

下面我们就看一下API文档

@property (nonatomic, readonly) NSInteger year; ///< Year component
@property (nonatomic, readonly) NSInteger month; ///< Month component (1~12)
@property (nonatomic, readonly) NSInteger day; ///< Day component (1~31)
@property (nonatomic, readonly) NSInteger hour; ///< Hour component (0~23)
@property (nonatomic, readonly) NSInteger minute; ///< Minute component (0~59)
@property (nonatomic, readonly) NSInteger second; ///< Second component (0~59)
@property (nonatomic, readonly) NSInteger nanosecond; ///< Nanosecond component
@property (nonatomic, readonly) NSInteger weekday; ///< Weekday component (1~7, first day is based on user setting)
@property (nonatomic, readonly) NSInteger weekdayOrdinal; ///< WeekdayOrdinal component
@property (nonatomic, readonly) NSInteger weekOfMonth; ///< WeekOfMonth component (1~5)
@property (nonatomic, readonly) NSInteger weekOfYear; ///< WeekOfYear component (1~53)
@property (nonatomic, readonly) NSInteger yearForWeekOfYear; ///< YearForWeekOfYear component
@property (nonatomic, readonly) NSInteger quarter; ///< Quarter component
@property (nonatomic, readonly) BOOL isLeapMonth; ///< whether the month is leap month
@property (nonatomic, readonly) BOOL isLeapYear; ///< whether the year is leap year
@property (nonatomic, readonly) BOOL isToday; ///< whether date is today (based on current locale)
@property (nonatomic, readonly) BOOL isYesterday; ///< whether date is yesterday (based on current locale)

下面详细的看一下这个API

1. @property (nonatomic, readonly) NSInteger year;

获取年

方法实现

- (NSInteger)year {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:self] year];
}

2. @property (nonatomic, readonly) NSInteger month

获取月

方法实现

- (NSInteger)month {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:self] month];
}

3. @property (nonatomic, readonly) NSInteger day

获取日

方法实现

- (NSInteger)day {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:self] day];
}

4. @property (nonatomic, readonly) NSInteger hour;

获取小时

方法实现

- (NSInteger)hour {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitHour fromDate:self] hour];
}

5. @property (nonatomic, readonly) NSInteger minute;

获取分

方法实现

- (NSInteger)minute {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitMinute fromDate:self] minute];
}

6. @property (nonatomic, readonly) NSInteger second;

获取秒

方法实现

- (NSInteger)second {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitSecond fromDate:self] second];
}

7. @property (nonatomic, readonly) NSInteger nanosecond;

获取纳秒

方法实现

- (NSInteger)nanosecond {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitSecond fromDate:self] nanosecond];
}

8. @property (nonatomic, readonly) NSInteger weekday;

获取工作日

方法实现

- (NSInteger)weekday {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:self] weekday];
}

9. @property (nonatomic, readonly) NSInteger weekdayOrdinal;

获取weekdayOrdinal

方法实现

- (NSInteger)weekdayOrdinal {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekdayOrdinal fromDate:self] weekdayOrdinal];
}

10. @property (nonatomic, readonly) NSInteger weekOfMonth;

获取weekOfMonth

方法实现

- (NSInteger)weekOfMonth {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekOfMonth fromDate:self] weekOfMonth];
}

11. @property (nonatomic, readonly) NSInteger weekOfYear;

获取一年的week序号。

方法实现

- (NSInteger)weekOfYear {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekOfYear fromDate:self] weekOfYear];
}

12. @property (nonatomic, readonly) NSInteger yearForWeekOfYear;

获取yearForWeekOfYear

方法实现

- (NSInteger)yearForWeekOfYear {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitYearForWeekOfYear fromDate:self] yearForWeekOfYear];
}

13. @property (nonatomic, readonly) NSInteger quarter;

获取quarter

方法实现

- (NSInteger)quarter {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitQuarter fromDate:self] quarter];
}

14. @property (nonatomic, readonly) BOOL isLeapMonth;

获取是否是闰月。

方法实现

- (BOOL)isLeapMonth {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitQuarter fromDate:self] isLeapMonth];
}

15. @property (nonatomic, readonly) BOOL isLeapYear;

获取是否是闰年。

方法实现

- (BOOL)isLeapYear {
    NSUInteger year = self.year;
    return ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0)));
}

16. @property (nonatomic, readonly) BOOL isToday;

获取是否是今天。

方法实现

- (BOOL)isToday {
    if (fabs(self.timeIntervalSinceNow) >= 60 * 60 * 24) return NO;
    return [NSDate new].day == self.day;
}

17. @property (nonatomic, readonly) BOOL isYesterday;

获取是否是昨天。

方法实现

- (BOOL)isYesterday {
    NSDate *added = [self dateByAddingDays:1];
    return [added isToday];
}

后记

本篇主要介绍了NSDate分类的Component Properties部分,感兴趣的给个赞或者关注~~~~

YYKit源码探究(五十三) —— NSDate分类之Component Properties(一)_第2张图片

你可能感兴趣的:(YYKit源码探究(五十三) —— NSDate分类之Component Properties(一))