2

我对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];

}

我可以提供您可能需要的任何其他信息,此时我正在撕毁我的头发。我只是不明白为什么时间没有更新..

4

1 回答 1

1

所以问题是 [[NSDate date] timeIntervalSince1970] 返回一个双精度数,当你将它截断为浮点数时,你会得到这个不正确的值。对所有浮点类型使用双精度,问题就会消失:

    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
于 2012-07-28T14:13:41.120 回答