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 8332039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:36:11+00:00 2026-06-09T02:36:11+00:00

I’m fairly new to iOS development so I’m sorry if this comes off as

  • 0

I’m fairly new to iOS development so I’m sorry if this comes off as a little silly.

but for the life of me I can’t figure out why when I call (NSTimeInterval)timeIntervalSince1970 inside a Loop in Objective-C it gets back the value on the first run and doesn’t update at all from there. I made the console output the current time whilst in the loop and its always the same:

I used this to print it:

NSLog([NSString stringWithFormat:@"time: %f",(float)[[NSDate date] timeIntervalSince1970]]);

and heres the console Output:

    GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 15 16:03:10 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".Attaching to process 557.
2012-07-28 13:17:01.801 QuitSmoking SideKick[557:207] number of times: 24 this app has been launched
2012-07-28 13:17:01.805 QuitSmoking SideKick[557:207] LOADED VIEW CONTROLLER
2012-07-28 13:17:01.806 QuitSmoking SideKick[557:207] time: 1343477760.000000
2012-07-28 13:17:02.807 QuitSmoking SideKick[557:207] time: 1343477760.000000
2012-07-28 13:17:03.807 QuitSmoking SideKick[557:207] time: 1343477760.000000
2012-07-28 13:17:04.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:05.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:06.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:07.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:08.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:09.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:10.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:11.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:12.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:13.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:14.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:15.807 QuitSmoking SideKick[557:207] time: 1343477888.000000
2012-07-28 13:17:16.807 QuitSmoking SideKick[557:207] time: 1343477888.000000

My Iphone app counts the amount of money you’ve saved since you’ve first installed the app. This has a calculating function that is called every iteration of the loop called [calulateMoney] which takes the time that user started from NSUserDefaults and puts it against the current time with other factors.

I start the loop with the following code which is ran at -(void)ViewDidLoad:

    NSRunLoop *runloop = [NSRunLoop currentRunLoop];

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(calculateMoney) userInfo:nil repeats:YES];

[runloop addTimer:timer forMode:NSRunLoopCommonModes];
[runloop addTimer:timer forMode:UITrackingRunLoopMode];

and finally here is the method (sorry if this is the wrong term, I’m usually Java) I call to calculate money:

    //method that performs math on the score against the cost of a smoke and how many seconds have passed.
-(IBAction) calculateMoney
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *packSize = [defaults objectForKey:@"packSize"];

    NSString *packCost = [defaults objectForKey:@"packCost"];

    NSString *dailyCigs = [defaults objectForKey:@"dailyCigs"];

    NSDate *timer_date = [NSDate date];

   // float current_time = [[NSDate date] timeIntervalSince1970];

    int size = [packSize intValue];
    int daily = [dailyCigs intValue];
    float cost = [packCost floatValue];

    NSLog([NSString stringWithFormat:@"time: %f",(float)[[NSDate date] timeIntervalSince1970]]);

    //current time doesn't log further than the time it's initialized. RESEARCH NEEDED.
    float seconds =  (float)[[NSDate date] timeIntervalSince1970] -[[defaults objectForKey:@"firstLogTime"]floatValue];

    float calcPart1 = (float) daily/size;
  //  NSLog([NSString stringWithFormat:@"calc 1: %f",calcPart1]);
    float calcPart2 = (float) calcPart1 * cost;
  //  NSLog([NSString stringWithFormat:@"calc 2: %f",calcPart2]);
    float calcPart3 = (float) calcPart2 / (24*60*60);
 //   NSLog([NSString stringWithFormat:@"calc 3: %f",calcPart3]);
    float result = (float) calcPart3 * seconds;
  //  NSLog([NSString stringWithFormat:@"result: %f",result]);


    money.text= [NSString stringWithFormat:@"%f", result];

    [money sizeToFit];

}

I can provide any other information you may need for this, I’m tearing my hair out at this point. I just don’t understand why the time isn’t getting updated..

  • 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-09T02:36:12+00:00Added an answer on June 9, 2026 at 2:36 am

    So the issue is that [[NSDate date] timeIntervalSince1970] returns a double, and when you truncate it to a float you get this incorrect value. Use doubles for all your floating point types and the problem will go away:

        NSLog(@"time: %lf", [[NSDate date] timeIntervalSince1970]);
    
    
    2012-07-28 11:17:42.200 TimeTester[22022:f803] time: 1343488662.200260
    2012-07-28 11:17:43.200 TimeTester[22022:f803] time: 1343488663.200198
    2012-07-28 11:17:44.200 TimeTester[22022:f803] time: 1343488664.200264
    2012-07-28 11:17:45.200 TimeTester[22022:f803] time: 1343488665.200141
    2012-07-28 11:17:46.200 TimeTester[22022:f803] time: 1343488666.200046
    2012-07-28 11:17:47.199 TimeTester[22022:f803] time: 1343488667.199891
    2012-07-28 11:17:48.200 TimeTester[22022:f803] time: 1343488668.200030
    2012-07-28 11:17:49.199 TimeTester[22022:f803] time: 1343488669.199775
    2012-07-28 11:17:50.199 TimeTester[22022:f803] time: 1343488670.199782
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
This could be a duplicate question, but I have no idea what search terms
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.