ios中2个日期的差异

时间:2022-01-19 16:54:45

I have an app where content is displayed to the user. I now want to find out how many seconds a user actually views that content for. So in my header file, I've declared an

我有一个应用程序,内容显示给用户。现在我想知道用户实际查看该内容的时间有多长。在头文件中,我声明了an

 NSDate *startTime;
 NSDate *endTime;

Then in my viewWillAppear

然后在我的那些

 startTime = [NSDate date];

Then in my viewWillDisappear

那么在我viewWillDisappear

endTime = [NSDate date];
NSTimeInterval secs = [endTime timeIntervalSinceDate:startTime];
NSLog(@"Seconds --------> %f", secs);

However, the app crashes, with different errors sometimes. Sometimes it's a memory leak, sometimes it's a problem with the NSTimeInterval, and sometimes it crashes after going back to the content for a second time.

然而,应用程序崩溃了,有时会出现不同的错误。有时是内存泄漏,有时是NSTimeInterval的问题,有时会在返回内容后再次崩溃。

Any ideas on to fix this?

有办法解决这个问题吗?

2 个解决方案

#1


13  

since you are not using ARC, when you write

因为你写的时候不用圆弧

startTime = [NSDate date];

开始时间=(NSDate日期);

you do not retain startTime, so it is deallocated before -viewWillDisappear is called. Try

您不保留开始时间,因此在调用-viewWillDisappear之前释放它。试一试

startTime = [[NSDate date] retain];

startTime = [[NSDate date]保留];

Also, I recommend to use ARC. There should be much less errors with memory management with it, than without it

另外,我建议使用ARC。内存管理的错误应该比没有内存管理少得多

#2


11  

You should declare a property with retain for the start date. Your date is getting released before you can calculate the time difference.

您应该在开始日期声明一个带有retain的属性。在你计算时差之前,你的约会就被释放了。

So declare

所以声明

@property (nonatomic, retain) NSDate *startDate

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setStartDate: [NSDate date]];
}

- (void)viewWillDisappear:(BOOL)animated
{
     [super viewWillDisappear:animated];
     NSLog(@"Seconds --------> %f",[[NSDate date] timeIntervalSinceDate: self.startDate]);
}

Don't forget to cleanup.

不要忘记清理。

- (void)dealloc
{
    [self.startDate release];
    [super dealloc];
}

#1


13  

since you are not using ARC, when you write

因为你写的时候不用圆弧

startTime = [NSDate date];

开始时间=(NSDate日期);

you do not retain startTime, so it is deallocated before -viewWillDisappear is called. Try

您不保留开始时间,因此在调用-viewWillDisappear之前释放它。试一试

startTime = [[NSDate date] retain];

startTime = [[NSDate date]保留];

Also, I recommend to use ARC. There should be much less errors with memory management with it, than without it

另外,我建议使用ARC。内存管理的错误应该比没有内存管理少得多

#2


11  

You should declare a property with retain for the start date. Your date is getting released before you can calculate the time difference.

您应该在开始日期声明一个带有retain的属性。在你计算时差之前,你的约会就被释放了。

So declare

所以声明

@property (nonatomic, retain) NSDate *startDate

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setStartDate: [NSDate date]];
}

- (void)viewWillDisappear:(BOOL)animated
{
     [super viewWillDisappear:animated];
     NSLog(@"Seconds --------> %f",[[NSDate date] timeIntervalSinceDate: self.startDate]);
}

Don't forget to cleanup.

不要忘记清理。

- (void)dealloc
{
    [self.startDate release];
    [super dealloc];
}