我是否需要在此代碼示例中釋放NSCalendar對象?或者這會影響到最後一行代碼是否返回從「gregorian」變量派生的newDate的事實?我是否需要在此代碼示例中釋放NSCalendar對象?
#import "NSDateHelper.h"
@implementation NSDate(NSDateHelper)
-(NSDate *) setHour:(NSInteger)hour andMinute:(NSInteger)minute {
// Get Calendar for Existing Date
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: self];
// Set Hour and Minute
[components setHour: hour];
[components setMinute: minute];
[components setSecond: 00];
// Create resultant Date
NSDate *newDate = [gregorian dateFromComponents: components];
// Clean Up
[gregorian release]; // TODO: Do I release this here, or will it affect the return value not being valid?
return newDate;
}
@end
你不應該使用'NSUIntegerMax'作爲dateFlags。計算所有不必要的東西需要更長的時間,而不僅僅是年月和日。 – 2011-03-19 00:55:40