私は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];
}
このために必要なその他の情報を提供できます。この時点で髪を引き裂いています。時間が更新されない理由がわかりません。