Using static NSDate to determine transit time

I am trying to set a static date to determine how many seconds elapsed between each call to this method.

Instant failure!

- (void) myMethod
{   
    static NSDate * staticDate = nil;
    NSTimeInterval seconds = 0.0;

    if (staticDate)
    {   
        NSLog (@ "% @", staticDate);
        seconds = [staticDate timeIntervalSinceNow];
    }

    staticDate = [NSDate date];
    NSLog (@ "%. 2f", seconds);
}
+5
source share
4 answers

, timeIntervalSince1970, . , 1 1970 .

, timeInterval , [[NSDate date] timeIntervalSince1970], :

-(void) myMethod
{
    NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970] - _initialTimeInterval;
    NSLog(@"Seconds = %.2f", seconds);
}

, , [NSDate date] . , , , , , .

, , , date. , / , , .

+7

?

NSLog(@"%.2f", seconds);

NSTimeInterval typedef double, placeholder float.

0

- :

-(void) myMethod
{   

     static NSDate *staticDate = nil;
     if (staticDate == nil) staticDate = [[NSDate date] retain];

     if(staticDate)
     {   
     NSTimeInterval elapsedTime = fabs([staticDate timeIntervalSinceNow]);
     NSLog(@"elapsedTime == %.7f sec (%.4f ms)",
                    elapsedTime, elapsedTime * 1000.0); 
     }
     [staticDate release];
     staticDate = [[NSDate date] retain];
}
0

- :

staticDate = [[NSDate date] retain];

, , , .

0

All Articles