jjzjj

objective-c - iOS 开发 : timeIntervalSince1970 not updating when in a NSRunLoop

coder 2024-01-27 原文

我是 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/

有关objective-c - iOS 开发 : timeIntervalSince1970 not updating when in a NSRunLoop的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  3. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  4. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  5. Ruby Sinatra 配置用于生产和开发 - 2

    我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm

  6. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  7. ruby - 是否可以覆盖 gemfile 进行本地开发? - 2

    我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI

  8. ruby - 在 Windows 机器上使用 Ruby 进行开发是否会适得其反? - 2

    这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby​​-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub

  9. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  10. ruby-on-rails - 在 Rails 开发环境中为 .ogv 文件设置 Mime 类型 - 2

    我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain

随机推荐