Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8038447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:14:20+00:00 2026-06-05T03:14:20+00:00

I have an in value set to 10. A then run a NSTimer with

  • 0

I have an in value set to 10. A then run a NSTimer with this code:
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];

My void look like this

  -(void)updateTimer {
         timeLeft = timeLeft -0.01;
         NSString *string = [NSString stringWithFormat:@"%i",timeLeft];
         [timer setString:string];
         NSLog(string);

     }

I am using Coco2D, this is not the problem
In my log it comes out like this

2012-06-05 17:28:56.030 NumberPop[31291:10a03] 9
2012-06-05 17:28:57.030 NumberPop[31291:10a03] 8
2012-06-05 17:28:58.030 NumberPop[31291:10a03] 7
2012-06-05 17:28:59.030 NumberPop[31291:10a03] 6
2012-06-05 17:29:00.030 NumberPop[31291:10a03] 5
2012-06-05 17:29:01.030 NumberPop[31291:10a03] 4
2012-06-05 17:29:02.030 NumberPop[31291:10a03] 3
2012-06-05 17:29:03.030 NumberPop[31291:10a03] 2
2012-06-05 17:29:04.029 NumberPop[31291:10a03] 1
2012-06-05 17:29:05.029 NumberPop[31291:10a03] 0
2012-06-05 17:29:06.029 NumberPop[31291:10a03] 0
2012-06-05 17:29:07.029 NumberPop[31291:10a03] 0
2012-06-05 17:29:08.030 NumberPop[31291:10a03] 0
2012-06-05 17:29:09.029 NumberPop[31291:10a03] 0
2012-06-05 17:29:10.030 NumberPop[31291:10a03] 0
2012-06-05 17:29:11.029 NumberPop[31291:10a03] 0
2012-06-05 17:29:12.029 NumberPop[31291:10a03] 0

The 0’s keep going on forever.
I am so lost, any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:14:22+00:00Added an answer on June 5, 2026 at 3:14 am

    So it’s curious as to why you’re trying to subtract .01 from an int. But when you do this, the result gets truncated to an int. So if you subtract .01 from 9 you get 8.99 but int can’t store 8.99 so it gets stored simply as 8. Once you get down to 0 it does the same thing. 0 – .01 = -0.01, which get truncated to 0. So you you’ll always get zero from that assignment.

    Instead, try something like this:

    timeLeft = timeLeft - 1;

    or even better

    timeLeft --;

    Either of the above will continue to decrement the integer below zero, ex: -1, -2, -3…. Assuming of course, that it’s an unsigned int (which int is).

    Also, if you’re looking to stop the timer at zero, you need to invalidate it when time left reaches zero. Look here for more info: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html#//apple_ref/occ/instm/NSTimer/invalidate

    Basically, do something like this:

    timeLeft --;
    if (timeLeft <= 0) [timer invalidate];
    

    There’s something important to understand though. NSTimer doesn’t guarantee that it will be called at exactly the time interval you specify. Basically, it checks on each run loop whether it should fire, and then does so if appropriate. But you can never fire a NSTimer more often than there are run loops. This means if a run loop takes longer than normal or your time interval is too short, it could skip several intervals before it fires. So counting ‘time’ by decrementing a number (like you seem to be doing) won’t work. Instead you’ll need to find another way, like setting a starting NSDate variable when you start the timer and then checking the current time on each timer fire against the start time.

    NSDate returns a fairly fine granularity in it’s calculations. So if you take a starting date, you can get the time interval since that date by doing this:

    NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:startDate];

    NSTimeInterval is a double. So you can convert it to a string like this:

    NSString *string = [NSString stringWithFormat:@"%f", interval];

    or to limit the decimal places:

    NSString *string = [NSString stringWithFormat:@"%.2f", interval];

    This should give you what you’re looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code $.post( /factory/set,{ key : value }, function(response) { });
I have the following PowerShell loop: Set-Variable -Name YELLOW -Option ReadOnly -Value 0 Set-Variable
I have been trying to set the value of a hidden input by using
I have a parameter @Department and set its value in the Dataset to =Join(Parameters!Department.Value,,)
I want to set my GtkComboBox to have some default value/name, on it as
I have a requirement to determine the maximum Id int value for a set
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
I have a PowerShell module called Test.psm1. I want to set a value on
As title, how can I set a table's column to have the default value
I have :set hlsearch as default value. When I search for something, search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.