月の設定は関係なく、別の操作です。日付の構成要素を取得し、日付を変更してから、日付を再構成します。これは、月を変更する前または後に行うことができます。
NSDate * today = [NSDate date];
NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents * offsetComponents = [NSDateComponents new];
[offsetComponents setMonth:-1];
// Either
NSDate * sameDayLastMonth = [cal dateByAddingComponents:offsetComponents
toDate:today
options:0];
NSDateComponents * lastMonthComps = [cal components:NSUIntegerMax
fromDate:sameDayLastMonth];
[lastMonthComps setDay:15];
NSDate * fifteenthOfLastMonth = [cal dateFromComponents:lastMonthComps];
// or
NSDateComponents * todayComps = [cal components:NSUIntegerMax
fromDate:today];
[todayComps setDay:15];
NSDate * fifteenth = [cal dateFromComponents:todayComps];
NSDate * fifteenthOfLastMonth = [cal dateByAddingComponents:offsetComponents
toDate:fifteenth
options:0];
または、さらに簡単です(ええと、私はそもそもこれを行うべきでした):
NSDate * today = [NSDate date];
NSCalendar * cal = [NSCalendar currentCalendar];
NSInteger offset = arc4random_uniform(2) ? -1 : 1;
NSDateComponents * comps = [cal components:NSUIntegerMax
fromDate:today];
[comps setDay:15];
[comps setMonth:[comps month] + offset];
NSDate * newDate = [cal dateFromComponents:comps];