在NSUserDefaults中存储NSDate的最佳方法是什么? [英] What's the optimum way of storing an NSDate in NSUserDefaults?

查看:812
本文介绍了在NSUserDefaults中存储NSDate的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



选项1 - setObject:forKey:


$

b $ b

  //设置
NSDate * myDate = [NSDate date];
[[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@myDateKey];

// Get
NSDate * myDate =(NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@myDateKey];



选项2 - timeIntervalSince1970



  // Set 
NSDate * myDate = [NSDate date];
NSTimeInterval myDateTimeInterval = [myDate timeIntervalSince1970];
[[NSUserDefaults standardUserDefaults] setFloat:myDateTimeInterval forKey:@myDateKey];

// Get
NSTimeInterval myDateTimeInterval = [[NSUserDefaults standardUserDefaults] floatForKey:@myDateKey];
NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:myDateTimeInterval];



优点和缺点



选项1



这似乎是紧凑和逻辑的。但是,由于日期格式化程序错误



选项2



这似乎很笨。我也不确定它的准确性 - 在一个测试,我做了,当我检索的日期,它出48秒,尽管苹果文档说NSTimeInterval具有亚秒精度。



需求



无论我选择什么方法,都必须是:


  1. 精确到一秒钟。


  2. 可读性高。

    ol>

    我的问题



    选项2的不准确性是因为我做错了什么?



    您会使用这两个选项中的哪一个?



    有没有另一个我不知道的选项?



    谢谢!

    解决方案

    为什么要将日期转换为时间间隔(然后将时间间隔转换为不同的原语)?只需 [sharedDefaults setObject:theDate forKey:@theDateKey] 并使用它。 NSDate是PLIST格式(日期,数字,字符串,数据,字典和数组)支持的主要类型之一,因此您可以直接存储它。



    请参阅证明文件



    只需直接存储和检索日期,并观看它做正确的事区域,精度等)。没有涉及到格式器,正如其他人所说的。


    There's two ways of storing an NSDate in NSUserDefaults that I've come across.

    Option 1 - setObject:forKey:

    // Set
    NSDate *myDate = [NSDate date];
    [[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@"myDateKey"];
    
    // Get
    NSDate *myDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"myDateKey"];
    

    Option 2 - timeIntervalSince1970

    // Set
    NSDate *myDate = [NSDate date];
    NSTimeInterval myDateTimeInterval = [myDate timeIntervalSince1970];
    [[NSUserDefaults standardUserDefaults] setFloat:myDateTimeInterval forKey:@"myDateKey"];
    
    // Get
    NSTimeInterval myDateTimeInterval = [[NSUserDefaults standardUserDefaults] floatForKey:@"myDateKey"];
    NSDate *myDate = [NSDate dateWithTimeIntervalSince1970:myDateTimeInterval];
    

    Pros and Cons

    Option 1

    This seems to be compact and logical. However, I've got worries about this going wrong because of Date Formatter bugs.

    Option 2

    This seems to be clumsy. I'm also unsure of the accuracy of it - in one test I did, when I retrieved the date back it was out by 48 seconds, despite the Apple Docs saying that NSTimeInterval has "subsecond accuracy".

    Requirements

    Whatever method I choose, it must be:

    1. Precise to within a second.

    2. Readable and reliable.

    My Question

    Is the inaccuracy with Option 2 because I'm doing something wrong?

    Which out of these two options would you use?

    Is there another option that I'm not aware of?

    Thanks!

    解决方案

    You are needlessly complicating things. Why are you converting the date to a time interval (then the time interval to a different primitive)? Just [sharedDefaults setObject:theDate forKey:@"theDateKey"] and be done with it. NSDate is one of the "main types" supported by the PLIST format (dates, numbers, strings, data, dictionaries, and arrays), so you can just store it directly.

    See the documentation for proof.

    Just store and retrieve the date directly and watch it Do The Right Thing (including time zones, precision, etc). There is no formatter involved, as others have said.

    这篇关于在NSUserDefaults中存储NSDate的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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