【问题标题】:-[__NSDate timeIntervalSinceReferenceDate]: message sent to deallocated-[__NSDate timeIntervalSinceReferenceDate]:消息发送到deallocated
【发布时间】:2011-10-20 04:48:28
【问题描述】:

我正在开发一个 iphone 应用程序,其中计算两个日期之间的天数我使用的代码如下:

    double intevalInSeconds = [selectedDate2 timeIntervalSinceDate:newfromdate];
int dayInSeconds = 60 * 60 * 24;
int day = intevalInSeconds / dayInSeconds;
NSLog(@"counted days are:%d",day);
NSDateFormatter *df=[[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:     
    NSGregorianCalendar];
NSDateComponents *days = [[NSDateComponents alloc] init];
NSInteger dayCount = 0;
while ( TRUE ) {
    [days setDay: ++dayCount];
    NSDate *date = [gregorianCalendar dateByAddingComponents: days toDate: newfromdate 
            options: 0];
    if ( [date compare: selectedDate2] == NSOrderedDescending )
        break;
    NSString *dateForAdd=[df stringFromDate:date];
    [countdays addObject:dateForAdd];
}
NSLog(@"daysdetail:%@",countdays);
appendarray = countdays;
NSLog(@"append array is:%@",appendarray);
}

当我在模拟器上运行此应用程序时,它运行良好,但每当我尝试在设备上运行它时,它都会给出错误|:- [__NSDate timeIntervalSinceReferenceDate]: message sent to deallocated instance 0x65a30b0

如果我在上面的代码中做错了,请告诉我。

非常感谢。

【问题讨论】:

  • 您在哪里创建 selectedDate2 并保留它?如果selectedDate2是在当前方法之外创建的,你应该保留它。
  • 你没有调用那个方法。你在哪里得到崩溃,意味着哪一行?

标签: iphone


【解决方案1】:

timeIntervalSinceDate: 呼叫timeIntervalSinceReferenceDate

因此,您的代码似乎在您向我们展示的代码之前的某处释放了一个 NSDates。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-26
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    相关资源
    最近更新 更多