在iphone sdk中日期的NSTimeinterval问题

时间:2011-02-09 14:00:59

标签: objective-c ios

我正在将日期转换为NSTimeinterval,如下面的代码:

NSDate *currentDate = [NSDate date];
NSTimeInterval currentyInterval = [appDelegate setTimeInterval:currentDate];    
NSDate *expiryDate = [appDelegate setReverseDate:warrObj.expiredOn];


NSTimeInterval expiryInterval = [appDelegate setTimeInterval:expiryDate];
//Here I am getting the nil value and exception is generated.
//It happens only in 3.1


-(NSTimeInterval )setTimeInterval:(NSDate *)selectedDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init] ;
    [dateFormatter setDateFormat:@"dd-MMM-yyyy 00:00:00"];
    NSString *currStr = [dateFormatter stringFromDate:selectedDate];
    NSDate *newCurrentDate = [dateFormatter dateFromString:currStr];

    NSTimeInterval timeInterval = [newCurrentDate timeIntervalSinceReferenceDate];
    [dateFormatter release];
    return timeInterval;
}

任何人都可以建议我如何摆脱这个。

感谢所有人, 马丹。

1 个答案:

答案 0 :(得分:0)

NSTimeInterval只是typedef double,它不是对象:

获取时间间隔:

NSTimerInterval timeInterval = [[NSDate date] timeIntervalSinceReferenceDate];


NSDate *expiryDate = [appDelegate setReverseDate:warrObj.expiredOn];
NSTimeInterval expiryInterval = [expiryDate timeIntervalSinceReferenceDate];

从时间间隔创建日期:

NSTimeInterval oneHour = 60*60;
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:timeInterval - oneHour];