所以我在我的应用程序中使用Brock Wolfs的答案来计时一个动作。实际时间是从我向服务器发送请求时使用的
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]);
//..
}
任何帮助将不胜感激。