管理対象オブジェクト(このオブジェクトの他のプロパティに基づいて日付を計算する)にカスタム計算メソッドを追加したいと思います。
これを一時的な属性としてコーディングするのが良いのか、カテゴリ内のプロパティをこの管理対象オブジェクトに追加するのが良いのかわかりません。
どう思いますか?
これは私の現在のコード(カテゴリ)です:
.h:
@interface IBFinPeriod (DateCalculations)
@property (readonly) NSDate* periodBeginDate;
@end
.m:
#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