gpt4 book ai didi

ios - 使用自定义 Timer NSObject 在你的 ios 应用程序中计时事件

转载 作者:行者123 更新时间:2023-11-29 11:05:32 25 4
gpt4 key购买 nike

所以我正在使用 Brock Wolfs回答在我的应用程序中安排一个 Action 。我的实际时间是从我使用

向我的服务器发送请求时开始的
NSURLConnection

直到我完成从

中的那个请求下载所有数据的那一刻
connectionDidFinishLoading

但是由于某些奇怪的原因,这个解决方案不起作用,它一直以 0.00 秒回复..即使实际事件使用断点大约需要 8 秒。

这是我的 Brock 代码版本(精确副本)

定时器.h

#import <Foundation/Foundation.h>

@interface Timer : NSObject{
NSDate *start;
NSDate *end;
}

- (void) startTimer;
- (void) stopTimer;
- (double) timeElapsedInSeconds;
- (double) timeElapsedInMilliseconds;
- (double) timeElapsedInMinutes;


@end

定时器.m

#import "Timer.h"

@implementation Timer

- (id) init {
self = [super init];
if (self != nil) {
start = nil;
end = nil;
}
return self;
}

- (void) startTimer {
start = [NSDate date];
}

- (void) stopTimer {
end = [NSDate date];
}

- (double) timeElapsedInSeconds {
return [end timeIntervalSinceDate:start];
}

- (double) timeElapsedInMilliseconds {
return [self timeElapsedInSeconds] * 1000.0f;
}

- (double) timeElapsedInMinutes {
return [self timeElapsedInSeconds] / 60.0f;
}

@end

然后在类里面我要使用它

我的类.h

#import "Timer.h"

Timer *timer;
//..
@property (strong, nonatomic) Timer *timer;

我的类.m

@synthesize timer;
//..

- (IBAction)vehicleSearchRequest:(NSData *)postBodyData
{
//..
[timer startTimer];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

//..
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//..
[timer stopTimer]; // comes back as 0.000

NSLog(@"Total time was: %lf milliseconds", [timer timeElapsedInMilliseconds]);
//..
}

如有任何帮助,我们将不胜感激。

最佳答案

它不起作用的原因一点都不奇怪。您从未创建过 Timer 实例。在 [timer timerStart] 之前插入此行:

self.timer = [[Timer alloc] init];

关于ios - 使用自定义 Timer NSObject 在你的 ios 应用程序中计时事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13794294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com