两次发射之间的时间间隔,iPhone [英] Time passed between launches, iPhone

查看:50
本文介绍了两次发射之间的时间间隔,iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个静态的时间指示器,该指示器从0开始计数,直到时间过去.经过一定天数后,我制作的游戏将会更改.我敢肯定这是一个简单的问题,但是即使在两次发射之间,我如何存储经过的总时间呢?

I'd like a static time indicator that counts from 0 to whatever as time passes. The game I'm making will change after a certain number of days has passed. I'm sure this is a simple question, but how I can I store the total time passed, even between launches?

所以...,第一次启动,时间= 0;
一小时后第二次发射时间= 60;
一天之后的第三次发射,时间= 86460;

So..., first launch, time = 0;
Second launch an hour later time = 60;
Third launch, a day later, time = 86460;

依此类推,假设此示例中没有花任何时间在应用程序中,但是当然,我希望它在打开应用程序时也保持递增.我以为我必须将值存储在某个地方,然后将其与(当前时间-上一次)进行比较,然后将数字相加,但是我不确定如何执行此操作.谢谢.

and so on, just assume no time was spent in the app for this example, but of course, I'd like it to keep incrementing while they app is open as well. I'd imagine I have to store a value somewhere, then compare it to the (current time - last time) and add the numbers together, but I'm not sure how to do this. Thanks.

推荐答案

使用NSUserDefaults存储从密钥中首次启动以来经过的时间(以秒为单位)以及应用程序上次启动的日期和时间.另一个关键.启动应用程序时,请读取值,并将当前日期和时间与上一个存储的日期和时间之间的差加到该数量上.然后,将新的更新值写入默认值,以便下次应用启动时再次阅读.

Use NSUserDefaults to store the amount of time in seconds that has passed from the first launch in a key and the date and time of the last launch of your app in another key. When you start your app, read the values and add to the amount the difference between the current date and time and the previous stored one. Then, write to the defaults the new updated values, for you to read again the next time your app starts.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

int timePassed = [[NSUserDefaults standardUserDefaults] intForKey:@"timePassed"];
NSDate *dateOfLastLaunch = [NSDate dateWithString: [[NSUserDefaults standardUserDefaults] stringForKey:@"dateOfLastLaunch"];

//Adds the time passed since the last launch
timePassed += [NSDate timeIntervalSinceDate: dateOfLastLaunch];

//Saves the new values
[defaults setObject:[NSNumber numberWithInt: timePassed] forKey:@"timePassed"];
[defaults setObject:[NSDate date] forKey:@"dateOfLastLaunch"];

这篇关于两次发射之间的时间间隔,iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆