NSTimer init problem

Discussion in 'iOS Programming' started by aukemid, Nov 20, 2010.

  1. aukemid macrumors newbie

    Mar 26, 2010
    I have the following code to start a timer:
    self.timer = [NSTimer scheduledTimerWithTimeInterval: (60 / 118)
    			target: self
    			selector: @selector(timerFiredMethod)
    			userInfo: nil
    			repeats: YES];
    This does not work!
    The result of 60 / 118 = 0.5084745763. If I enter this result as interval it does work. Can someone tell me why this is?
  2. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    If you divide two integers you will get an integer result.

    Try 60.0 / 180.0.
  3. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    60 and 118 are integer constants. Therefore, their type is int, and all arithmetic will be performed with integer quantities. So calculated as an integer, 60/118 is 0.

    If you want fractional quantities, you must use numbers that have fractional parts, even if the fractional part is 0. A fractional constant is one containing a decimal point. Example: 60.0 / 118.0.

    The fractional types are 'double' and 'float'. Here, I use the word "fractional" to mean "capable of representing a fraction". Clearly, the integer types (char, int, long) do not have this capability, or they wouldn't be integer types.
  4. aukemid thread starter macrumors newbie

    Mar 26, 2010
    Thanks, that solved it.

Share This Page