Swift: NSDate losing sub-Second precision after loading

170 views Asked by At

I have a 2-dimensional array of NSDates, working at sub-second precision.

But when I save the array like this:

let pathes = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = pathes.objectAtIndex(0) as! NSString
let path = documentsDirectory.stringByAppendingPathComponent("tj_001.plist")
var dicti = NSMutableDictionary()

dicti.setObject(tDate, forKey: "tTimes")

dicti.writeToFile(path, atomically: false)

and load it like:

let dicti:AnyObject = NSDictionary(contentsOfFile: path)!

        . . .

                tDate = dicti.objectForKey("tTimes") as! [[(NSDate)]]

then every NSDate has lost its sub-second precision. Where is it ?

1

There are 1 answers

0
Ewan Mellor On BEST ANSWER

Someone reported this bug in 2009. http://openradar.appspot.com/6768646

It sounds like a fundamental flaw in the plist serialization (which you're implicitly using through NSDictionary.writeToFile).

You'll have to serialize these yourself. Saving the fractional part of the date as a separate NSNumber in your dictionary seems easy enough.