开发者

SystemTimers in Objective C

I need to find the difference between the last tick count and the current tick count, but it's printing the same value every time. The timer value does not change in here.Is there anything wrong with the code below?

SystemTimer.m:

#import "SystemTimers.h"


@implementation SystemTimers

-(id)init
{
    if (self=[super init]) {
        [self Reset];
    }
    return self;
}
+(unsigned long)GetTickCount
{
      return (unsigned long)[NSNumber numberWithLong:[[NSDate date] timeIntervalSince1970] * 1000];
    // NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate]*1000;
    //return (unsigned long)now;
}
-(unsigned long)Get:(BOOL)bUpdate
{
    unsigned long dwTickCount = [SystemTimers GetTickCount];
    NSLog(@"dwTickCount:%0.0f",dwTickCount);

    if ( bUpdate )
    {
        if ( dwTickCount < m_dwLastTickCount )
        {  
            m_dwTimer += (dwTickCount - m_dwLastTickCount) + ((unsigned long int)-1);
        }
        else
   开发者_如何学Python     {   
            m_dwTimer += dwTickCount - m_dwLastTickCount;
        }
        NSLog(@"m_dwTickCount:%0.0f  m_dwTimer:%0.0f",m_dwLastTickCount,m_dwTimer);

        m_dwLastTickCount = dwTickCount;
    }
    NSLog(@"m_dwTickCount:%0.0f  m_dwTimer:%0.0f",m_dwLastTickCount,m_dwTimer);
    return m_dwTimer;

}
-(void)Reset
{
    m_dwLastTickCount = [SystemTimers GetTickCount];
    m_dwTimer = 0;
}

@end

SystemTimer.h:

#import <Foundation/Foundation.h>


@interface SystemTimers : NSObject {
 @private
    unsigned long int m_dwLastTickCount;
    unsigned long int m_dwTimer;
}

-(id)init;
+(unsigned long)GetTickCount;
-(unsigned long)Get:(BOOL)bUpdate;
-(void)Reset;

@end

main.m:

#import <Foundation/Foundation.h>
#import "SystemTimers.h"
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    SystemTimers* timers = [[SystemTimers alloc]init];
    [timers Get:true];
    //[timers Reset];
    [pool drain];
    return 0;
}

Output:

2011-05-15 09:53:19.422 SystemTimer[341:a0f] dwTickCount:1100976
2011-05-15 09:53:19.424 SystemTimer[341:a0f] m_dwTickCount:1100976  m_dwTimer:1104

2011-05-15 09:53:19.422 SystemTimer[341:a0f] dwTickCount:1100976
2011-05-15 09:53:19.424 SystemTimer[341:a0f] m_dwTickCount:1100976  m_dwTimer:1104

2011-05-15 09:53:19.422 SystemTimer[341:a0f] dwTickCount:1100976
2011-05-15 09:53:19.424 SystemTimer[341:a0f] m_dwTickCount:1100976  m_dwTimer:1104

I hope it printing the address everytime and not the tick count.why is that happening?


You are doing some invalid typecasting in GetTickCount. You're typecasting an NSNumber object to an unsigned long. If you want milliseconds since 1970 you should instead just typecast the NSTimeInterval that is returned by timeIntervalSince1970:

+(unsigned long)GetTickCount
{
    return (unsigned long)([[NSDate date] timeIntervalSince1970] * 1000.0);
}

In your calls to NSLog, don't use %f for long integers. Use %ld instead:

NSLog(@"dwTickCount:%ld", dwTickCount);

I'm not exactly sure what you're trying to do in Get, but hopefully this will get you on the right track.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜