NSDate崩溃并发生连续动作 [英] NSDate crash with successive actions

查看:46
本文介绍了NSDate崩溃并发生连续动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有以下代码,该代码旨在将名为"today"的类var向前或向后更改一天.它会工作一次,但之后会崩溃.无论我按向左按钮还是向右按钮,它都将执行相同的操作.我在做什么错了?

I have the following code below that is meant to change a class var called "today" forward or backward by one day. It will work one time but then after that it crashes. It will do the same no matter if I press the left button or right button. What am I doing wrong?

今天的var是以.. today = [NSDate date]

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

这是崩溃的方法:

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

推荐答案

您不仅需要保留创建的日期,还需要释放今天"持有的现有值,否则您将泄漏旧的参考.

Not only do you need to retain the date created, but you also need to release the existing value held by "today," otherwise you'll leak the old reference.

初始化实例时,请使用:

When initializing the instance, use:

today = [[NSDate date] retain];

我将最后一行更改为:

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

最后,在您的dealloc方法中,添加:

And finally, in your dealloc method, add:

[today release];

在调用[super dealloc]之前;

before calling [super dealloc];

这篇关于NSDate崩溃并发生连续动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆