2011-05-27 24 views
0

我如何從appEnterBackground獲取日期並從appEnterForeground中拿走,然後顯示標籤中的差異。 這是到目前爲止我的代碼..iphone - 時間(應用程序隱藏)和時間(應用程序出現)之間的差異

**.h** 
    NSTimeInterval appEnteredBackground; 
    NSTimeInterval appEnteredForeground; 
    NSTimeInterval difference; 

**.m** 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    appEnteredBackground = [NSDate timeIntervalSinceReferenceDate]; 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    appEnteredForeground = [NSDate timeIntervalSinceReferenceDate]; 
    difference = appEnteredForeground - appEnteredBackground; 

    NSLog(@"Duration is %@",[NSDate dateWithTimeIntervalSinceReferenceDate: difference]); 
    NSLog(@"Duration is %@", [NSString stringWithFormat:@"%f", difference]); 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 

    NSString *time = [NSString stringWithFormat:@"%f", difference]; **//ERROR HERE (variable not used)** 

    [dateFormatter release]; 
} 

任何幫助將是非常美妙的

+0

什麼錯誤? – onnoweb 2011-05-27 14:59:41

+0

@onnoweb:** // ERROR HERE(變量未使用)**我想將'差異'的值存入標籤或字符串中,以便我可以在計算中使用它... – myles 2011-05-27 15:06:43

+0

[timeIntervalSinceNow help !!](http://stackoverflow.com/questions/6144966/timeintervalsincenow-help) – 2011-05-27 15:14:52

回答

0

我認爲你沒有錯誤在這裏。你正確地計算時間差,然後你用它的「描述」建立一個字符串,但不要使用它。 ,因爲你不積極使用它

NSLog(@"Computed time was: %@", time); 

你應該關心的是time變量:

看它所有的作品的方法結束前正確嘗試。

0

要測量此類事件(或事件差異),您需要分辨率小於1秒的東西。有一個「c」接口來獲取機器時間,這是處理器內部硬件自由運行的「滴答聲」。

您可以使用此StopWatch類獲得真的精確計時(毫秒,如果您認爲它們是微秒)。

StopWatch.h

#import <Foundation/Foundation.h> 


@interface StopWatch : NSObject 
{ 
    uint64_t _start; 
    uint64_t _stop; 
    uint64_t _elapsed; 
} 

-(void) Start; 
-(void) Stop; 
-(void) StopWithContext:(NSString*) context; 
-(double) seconds; 
-(NSString*) description; 
+(StopWatch*) stopWatch; 
-(StopWatch*) init; 
@end 

StopWatch.m

#import "StopWatch.h" 
#include <mach/mach_time.h> 

@implementation StopWatch 

-(void) Start 
{ 
    _stop = 0; 
    _elapsed = 0; 
    _start = mach_absolute_time(); 
} 
-(void) Stop 
{ 
    _stop = mach_absolute_time(); 
    if(_stop > _start) 
    { 
     _elapsed = _stop - _start; 
    } 
    else 
    { 
     _elapsed = 0; 
    } 
    _start = mach_absolute_time(); 
} 

-(void) StopWithContext:(NSString*) context 
{ 
    _stop = mach_absolute_time(); 
    if(_stop > _start) 
    { 
     _elapsed = _stop - _start; 
    } 
    else 
    { 
     _elapsed = 0; 
    } 
    NSLog([NSString stringWithFormat:@"[%@] Stopped at %f",context,[self seconds]]); 

    _start = mach_absolute_time(); 
} 


-(double) seconds 
{ 
    if(_elapsed > 0) 
    { 
     uint64_t elapsedTimeNano = 0; 

     mach_timebase_info_data_t timeBaseInfo; 
     mach_timebase_info(&timeBaseInfo); 
     elapsedTimeNano = _elapsed * timeBaseInfo.numer/timeBaseInfo.denom; 
     double elapsedSeconds = elapsedTimeNano * 1.0E-9; 
     return elapsedSeconds; 
    } 
    return 0.0; 
} 
-(NSString*) description 
{ 
    return [NSString stringWithFormat:@"%f secs.",[self seconds]]; 
} 
+(StopWatch*) stopWatch 
{ 
    StopWatch* obj = [[[StopWatch alloc] init] autorelease]; 
    return obj; 
} 
-(StopWatch*) init 
{ 
    [super init]; 
    return self; 
} 

@end 

類有一個靜態stopWatch方法,它返回一個autoreleased對象。

一旦您致電start,請使用seconds方法獲取已用時間。再次呼叫start重新啓動它。或stop停止它。在撥打stop後,您仍然可以隨時讀取時間(致電seconds)。

實施例在一個函數(執行時序呼叫)

-(void)SomeFunc 
{ 
    StopWatch* stopWatch = [StopWatch stopWatch]; 
    [stopWatch Start]; 

    ... do stuff 

    [stopWatch StopWithContext:[NSString stringWithFormat:@"Created %d Records",[records count]]]; 
} 
相關問題