iOS 7 注: NSUIntegerMax の使用が iOS7 で機能しなくなったようで、それを使用すると約 3 時間尻尾を追いかけたので、これらの質問のいくつかにこれを入れています。を使用するNSUIntegerMax
と、NSDateComponents
設定された年が無視されることがわかりました。たとえば、これは年を変更しません。
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:NSUIntegerMax fromDate:date];
[comps setYear:year];
return [gregorian dateFromComponents:comps];
これに対して:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger timeComps = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit);
NSDateComponents *comps = [gregorian components:timeComps fromDate:date];
[comps setYear:year];
return [gregorian dateFromComponents:comps];
これは iOS7 のバグであるか、NSUIntegerMax の使用がまぐれで機能しなくなった可能性があります。バグレポートを提出し、決定的な回答を得ます。
とにかく、予期しない結果を避けたい場合は、他のすべてのコンポーネントを指定してください。