Resolved Help with Making a Clock in Objective-C Command Line Utility

Discussion in 'Mac Programming' started by faroZ06, Aug 23, 2012.

  1. faroZ06, Aug 23, 2012
    Last edited: Aug 23, 2012

    faroZ06 macrumors 68040

    faroZ06

    Joined:
    Apr 3, 2009
    #1
    I'm playing around in Objective-C with a "foundation tool" (Objective-C command line utility) project in Xcode. At the end of my program, it's supposed to go to a clock that updates itself in the Terminal window. The problem is that it doesn't start on the right time, so it's almost always off by a fraction of a second. I tried to fix that by having it wait until the nanosecond time is a multiple of 10^9, but this ends up just going into infinite loop. What am I doing wrong, and is there a better way (I'm guessing there is)?

     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    [NSDate timeIntervalSinceReferenceDate] measures the time interval in seconds.

    And to avoid wasting CPU time, call [NSDate timeIntervalSinceReferenceDate] once, round up to the next second by calling ceil (), then call usleep for waiting the right amount of time.
     
  3. faroZ06 thread starter macrumors 68040

    faroZ06

    Joined:
    Apr 3, 2009
    #3
    Ah, thanks. Sorry for the noob question.
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    Nothing to be sorry about. You posted the code that caused the problem, which means you gave the information that was needed to find the cause of the problem - that puts you far ahead of the average.
     

Share This Page