Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iphone-时间(应用程序隐藏)和时间(应用程序出现)之间的差异_Iphone_Objective C_Nsstring_Xcode4 - Fatal编程技术网

iphone-时间(应用程序隐藏)和时间(应用程序出现)之间的差异

iphone-时间(应用程序隐藏)和时间(应用程序出现)之间的差异,iphone,objective-c,nsstring,xcode4,Iphone,Objective C,Nsstring,Xcode4,如何从appEnterBackground中获取日期并从appEnterForeground中删除,然后在标签中显示差异。 这是到目前为止我的代码 **.h** NSTimeInterval appEnteredBackground; NSTimeInterval appEnteredForeground; NSTimeInterval difference; **.m** - (void)applicationDidEnterBackground:(UIAppl

如何从appEnterBackground中获取日期并从appEnterForeground中删除,然后在标签中显示差异。 这是到目前为止我的代码

**.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];
}

任何帮助都会非常好

我认为您在这里没有错误。如果正确计算了时差,则可以构建一个带有“说明”的字符串,但不使用它

若要查看所有功能是否正常,请在方法结束前尝试以下操作:

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

你应该关心时间变量,因为你没有积极地使用它。

要测量这种类型的事件(或事件的差异),你需要分辨率小于一秒的东西。有一个“c”接口来获取机器时间,这是处理器内部无硬件运行的“滴答声”

使用这个秒表类,您可以获得非常好的计时(毫秒、微秒,如果您相信的话)

秒表.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

@onnoweb://ERROR HERE(变量未使用)我想将“difference”的值输入到标签或字符串中,以便我可以在计算中使用它……可能重复您已经问过的这个问题。再问一次也帮不了你。@乔纳森:只是迷路了,需要一些帮助。你应该阅读
NSDate
的文档。看起来你在胡乱尝试。文档告诉您像
+datewithtimeintervalcescereferencedate:
这样的方法可以做什么。
#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
-(void)SomeFunc
{
   StopWatch* stopWatch = [StopWatch stopWatch];
   [stopWatch Start];

   ... do stuff

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