NSDate Problem

I have the following code that is designed to change the var class, called today, forward or backward, for one day. It will work once, but after that it will work. He will do the same, regardless of whether I press the left button or the right button. What am I doing wrong?

var today is the var class, initiated as .. today = [NSDate date]

Here is the method that crashes:

 (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;

}

+3
source share
4 answers

You do not need to save the created date, but you also need to free the existing value stored "today", otherwise the old link will leak.

When initializing an instance, use:

today = [[NSDate date] retain];

:

[today release];
today = [newDay retain];

, , dealloc :

[today release];

[super dealloc];

+9

Maybe you need to say

today = [[NSDate date] retain]
+1
source

It seems to me that you need to save the newDay object returned by the addTimeInterval method. You may also need to be released today before completing the assignment at the end.

+1
source

Source: https://habr.com/ru/post/1710427/


All Articles