Confused By the Output of My Program

Discussion in 'iPhone/iPad Programming' started by Stunner, Nov 11, 2009.

  1. macrumors newbie

    Joined:
    Sep 14, 2009
    #1
    Ey guys, I am a little confused by the output I am getting from timeIntervalSinceDate. Here is the code I have:
    Code:
    NSDate *prevDate;
    	for (NSDate *thisDate in self.arrayOfTimes) {
    		NSLog(@"item: %@",thisDate);
    		if (counter > 1) {
    			NSLog(@"item: %d",[thisDate timeIntervalSinceDate:prevDate]);
    		}
    		prevDate = thisDate;
    	}
    
    And here is my output in console:
    Code:
    2009-11-11 05:25:39.740 WTC[82810:20b] counter: 1
    2009-11-11 05:25:39.742 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
    2009-11-11 05:25:41.087 WTC[82810:20b] counter: 2
    2009-11-11 05:25:41.088 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
    2009-11-11 05:25:41.089 WTC[82810:20b] item: 0
    2009-11-11 05:25:41.089 WTC[82810:20b] item: 2009-11-11 05:25:41 -0800
    2009-11-11 05:25:41.089 WTC[82810:20b] item: -1610612736
    2009-11-11 05:25:43.481 WTC[82810:20b] counter: 3
    2009-11-11 05:25:43.482 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
    2009-11-11 05:25:43.484 WTC[82810:20b] item: 0
    2009-11-11 05:25:43.485 WTC[82810:20b] item: 2009-11-11 05:25:41 -0800
    2009-11-11 05:25:43.485 WTC[82810:20b] item: -1610612736
    2009-11-11 05:25:43.486 WTC[82810:20b] item: 2009-11-11 05:25:43 -0800
    2009-11-11 05:25:43.486 WTC[82810:20b] item: -402653184
    2009-11-11 05:25:49.757 WTC[82810:20b] counter: 4
    2009-11-11 05:25:49.758 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
    2009-11-11 05:25:49.759 WTC[82810:20b] item: 0
    2009-11-11 05:25:49.760 WTC[82810:20b] item: 2009-11-11 05:25:41 -0800
    2009-11-11 05:25:49.761 WTC[82810:20b] item: -1610612736
    2009-11-11 05:25:49.761 WTC[82810:20b] item: 2009-11-11 05:25:43 -0800
    2009-11-11 05:25:49.762 WTC[82810:20b] item: -402653184
    2009-11-11 05:25:49.762 WTC[82810:20b] item: 2009-11-11 05:25:49 -0800
    2009-11-11 05:25:49.763 WTC[82810:20b] item: 671088640
    2009-11-11 05:25:53.821 WTC[82810:20b] counter: 5
    2009-11-11 05:25:53.822 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
    2009-11-11 05:25:53.822 WTC[82810:20b] item: 0
    2009-11-11 05:25:53.823 WTC[82810:20b] item: 2009-11-11 05:25:41 -0800
    2009-11-11 05:25:53.824 WTC[82810:20b] item: -1610612736
    2009-11-11 05:25:53.824 WTC[82810:20b] item: 2009-11-11 05:25:43 -0800
    2009-11-11 05:25:53.825 WTC[82810:20b] item: -402653184
    2009-11-11 05:25:53.826 WTC[82810:20b] item: 2009-11-11 05:25:49 -0800
    2009-11-11 05:25:53.826 WTC[82810:20b] item: 671088640
    2009-11-11 05:25:53.826 WTC[82810:20b] item: 2009-11-11 05:25:53 -0800
    2009-11-11 05:25:53.827 WTC[82810:20b] item: -1073741824
    2009-11-11 05:26:00.567 WTC[82810:20b] counter: 6
    2009-11-11 05:26:00.568 WTC[82810:20b] item: 2009-11-11 05:25:39 -0800
    2009-11-11 05:26:00.569 WTC[82810:20b] item: 0
    2009-11-11 05:26:00.569 WTC[82810:20b] item: 2009-11-11 05:25:41 -0800
    2009-11-11 05:26:00.570 WTC[82810:20b] item: -1610612736
    2009-11-11 05:26:00.570 WTC[82810:20b] item: 2009-11-11 05:25:43 -0800
    2009-11-11 05:26:00.571 WTC[82810:20b] item: -402653184
    2009-11-11 05:26:00.571 WTC[82810:20b] item: 2009-11-11 05:25:49 -0800
    2009-11-11 05:26:00.572 WTC[82810:20b] item: 671088640
    2009-11-11 05:26:00.573 WTC[82810:20b] item: 2009-11-11 05:25:53 -0800
    2009-11-11 05:26:00.574 WTC[82810:20b] item: -1073741824
    2009-11-11 05:26:00.575 WTC[82810:20b] item: 2009-11-11 05:26:00 -0800
    2009-11-11 05:26:00.575 WTC[82810:20b] item: -671088640
    
    I am wondering why the program is printing out positive and negative numbers such as: -671088640 and 671088640. And how would I go about converting these values into seconds? Thanks!
     
  2. macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #2
    timeIntervalSinceDate returns an NSTimeInterval, NSTimeInterval is a double, you are trying to print an integer... Overflow is biting you.

    Try:

    Code:
    NSLog(@"interval: %g",[thisDate timeIntervalSinceDate:prevDate]);
    
     
  3. thread starter macrumors newbie

    Joined:
    Sep 14, 2009
    #3
    Awesome, thank you. By the way, does there happen to be a page that lists what should be used when printing different values? Such as %d for integers, %g for time values, etc. Thanks again.
     
  4. macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #4
  5. thread starter macrumors newbie

    Joined:
    Sep 14, 2009
    #5
    Awesome, thanks.
     

Share This Page