jar*_*ryd 1 iphone time objective-c ios
在iOS中获取时间戳然后在例如2个时间戳上计算时间的最佳方法是什么?
它是什么数据类型?如果是NSTimeInterval,该值是否可以存储在字典中,然后写入plist?
Unix时间最好.实际上,使用NSTimeInterval可以很容易地计算日期.它只是一个双,所以:
NSDate *startDate;
NSDate *endDate;
NSTimeInterval interval;
// assuming start and end dates have a value
interval = [endDate timeIntervalSince1970] - [startDate timeIntervalSince1970];
Run Code Online (Sandbox Code Playgroud)
当要将字典序列化或写入Couch或Mongo等文档数据库时,我通常将时间间隔记录为NSString:
NSDate *dateToStore;
// assume dateToStore has a value
NSTimeInterval aTimeInterval = [dateToStore timeIntervalSince1970];
NSString *aStringObject = [NSString stringwithFormat:@"%f", aTimeInterval];
NSDictionary *aDict = [NSDictionary dictionaryWithObject:dateAsString
forKey:@"aKey"];
Run Code Online (Sandbox Code Playgroud)
然后,当您使用字典对象时,您可以再次阅读它:
NSTimeInterval newTimeInterval = [[aDict valueForKey:"aKey"] doubleValue];
NSDate *retreivedDate = [NSDate dateWithTimeIntervalSince1970:newTimeInterval];
Run Code Online (Sandbox Code Playgroud)
如果它是plist,你可以跳过字符串转换.
| 归档时间: |
|
| 查看次数: |
4537 次 |
| 最近记录: |