- (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)date
这个方法基本上都用过,获取当前NSDate对象之后,从中提取出NSDateComponents类型的某个元素。但是最近升级IOS8之后发现密密麻麻几十个报错,基本上都是在说'NSMonthCalendarUnit' is deprecated:first deprecated in iOS 8.0,百度了一下也没看到清楚的解决方方法,只好动手查Reference。略有收获,与大家分享一下。
解决问题的方法有两个。
方法一:在Apple的Reference里面给出。原文如下:
enum {
NSEraCalendarUnit = kCFCalendarUnitEra ,
NSYearCalendarUnit = kCFCalendarUnitYear ,
NSMonthCalendarUnit = kCFCalendarUnitMonth ,
NSDayCalendarUnit = kCFCalendarUnitDay ,
NSHourCalendarUnit = kCFCalendarUnitHour ,
NSMinuteCalendarUnit = kCFCalendarUnitMinute ,
NSSecondCalendarUnit = kCFCalendarUnitSecond ,
NSWeekCalendarUnit = kCFCalendarUnitWeek ,
NSWeekdayCalendarUnit = kCFCalendarUnitWeekday ,
NSWeekdayOrdinalCalendarUnit = kCFCalendarUnitWeekdayOrdinal ,
NSQuarterCalendarUnit = kCFCalendarUnitQuarter ,
NSWeekOfMonthCalendarUnit = kCFCalendarUnitWeekOfMonth ,
NSWeekOfYearCalendarUnit = kCFCalendarUnitWeekOfYear ,
NSYearForWeekOfYearCalendarUnit = kCFCalendarUnitYearForWeekOfYear
NSCalendarCalendarUnit = (1 << 20),
NSTimeZoneCalendarUnit = (1 << 21),
};
typedef NSUInteger NSCalendarUnit;
所以只要把左边的会报错的换成右边对应的枚举值就OK了。
方法二:xcode提示警告的时候旁边就会有提示,可能因为屏幕不够宽挡住,以NSMonthCalendarUnit为例,换成NSCalendarUnitMonth也就可以了