我有下面的代碼,這是爲了改變一個名爲「今天」的類var前進或後退一天。它會工作一次,但之後它崩潰。無論按下左鍵還是右鍵,它都會執行相同操作。我究竟做錯了什麼?NSDate問題
今天的變種是發起的。如今= [NSDate的日期]
這裏一類變種是,崩潰的方法:
(IBAction)changeDateByOne:(id)sender{
NSDate *newDay;
NSDate *currentDay = today;
NSTimeInterval secondsPerDay = 24 * 60 * 60;
if(sender == leftButton){
newDay = [currentDay addTimeInterval:-secondsPerDay];
}else if(sender == rightButton) {
newDay = [currentDay addTimeInterval: secondsPerDay];
}
today = newDay;
}
而不是`today = [[NSDate date] retain];`,最好是:`today = [[NSDate alloc] init];```````````````` – user102008 2011-04-19 21:52:00