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..
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: