作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 iOS 开发的新手,所以如果这有点愚蠢,我很抱歉。
但对于我的一生,我无法弄清楚为什么当我在 Objective-C 的循环中调用 (NSTimeInterval)timeIntervalSince1970 时,它会在第一次运行时取回值并且根本不会从那里更新。我在循环中使控制台输出当前时间,并且它始终相同:
我用这个来打印它:
NSLog([NSString stringWithFormat:@"time: %f",(float)[[NSDate date] timeIntervalSince1970]]);
这是控制台输出:
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
我的 Iphone 应用程序会统计您自首次安装该应用程序以来节省的金额。这有一个计算函数,该函数在称为 [calulateMoney] 的循环的每次迭代中调用,该函数使用用户从 NSUserDefaults 开始的时间并将其与当前时间和其他因素进行比较。
我使用在 -(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];
最后是计算金钱的方法(抱歉,如果这是错误的术语,我通常是 Java):
//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];
}
我可以提供您可能需要的任何其他信息,此时我正在撕扯我的头发。我只是不明白为什么时间没有更新..
最佳答案
所以问题是 [[NSDate date] timeIntervalSince1970] 返回一个 double 值,当您将其截断为 float 时,您会得到这个不正确的值。对所有浮点类型使用 double ,问题就会消失:
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
关于objective-c - iOS 开发 : timeIntervalSince1970 not updating when in a NSRunLoop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11701001/
我是一名优秀的程序员,十分优秀!