Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c iOS开发人员:在nsrunlop中时,TimeIntervalence1970未更新_Objective C_Ios_Timer_Nsdate - Fatal编程技术网

Objective c iOS开发人员:在nsrunlop中时,TimeIntervalence1970未更新

Objective c iOS开发人员:在nsrunlop中时,TimeIntervalence1970未更新,objective-c,ios,timer,nsdate,Objective C,Ios,Timer,Nsdate,我对iOS开发还比较陌生,所以如果这看起来有点愚蠢,我很抱歉 但就我的一生而言,我不明白为什么在Objective-C的循环中调用(NSTimeInterval)timeIntervalSince1970时,它会在第一次运行时返回值,并且根本不会从中更新。我使控制台在循环中输出当前时间,并且始终相同: 我用这个来打印它: NSLog([NSString stringWithFormat:@"time: %f",(float)[[NSDate date] timeIntervalSince1970

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

仔细看看您的控制台输出。我看到时间在更新。2012-07-28 13:17:03.807戒烟伙伴[557:207]时间:1343477760.000000 2012-07-28 13:17:04.807戒烟伙伴[557:207]时间:1343477888.000000是的,就一次。那就什么都没有了。这就是问题所在。所以你要说的是在应用程序委托中创建另一个方法,获取我在Money controller中调用的时差?我将自己测试此代码-我的答案不正确-我将在接下来的30分钟内解决它。是的!太好了!现在我只需要找到一个很好的方法来存储它。你可以将它存储为NSNUmber-[NSNUmber numberWithDouble:timeVal]如果我需要对它进行计算,比如用它来解决浮点值的数学问题,该怎么办?