我想将自定义计算方法添加到托管对象(根据该对象的其他属性计算某个日期)。
我不确定将其编码为瞬态属性或将类别中的属性添加到此托管对象是否更好。
你怎么看?
这是我当前的代码(类别):
。H:
@interface IBFinPeriod (DateCalculations)
@property (readonly) NSDate* periodBeginDate;
@end
米:
#import "IBFinPeriod+DateCalculations.h"
@implementation IBFinPeriod (DateCalculations)
- (NSDate*)periodBeginDate
{
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
if ([self.incPeriodTypeCode isEqualToString:@"M"]) {
offsetComponents.month = - [self.incPeriodLength intValue];
} else if ([self.incPeriodTypeCode isEqualToString:@"W"]) {
offsetComponents.week = - [self.incPeriodLength intValue];
}
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *beginDate = [calendar dateByAddingComponents:offsetComponents toDate:self.EndDate options:0];
return beginDate;
}
@end