问题
I believe this is a bug introduced in the new stack probably Swift 3 / iOS 10. I am running on XCode8 / Swift3 / iPhone 7 (Simulator) / ios 10.0 I have not found any related report of it.
set the value
let dt : Date? = Date() UserDefaults.standard.set(dt, forKey: "test-date")Get the value
let dt = UserDefaults.standard.object(forKey: "test-date") if dt == nil { return false } else { let date = dt! as Date // FAILED }
The failed message is:
Could not cast value of type '__NSCFData' (0x10fa08500) to 'NSDate' (0x10fa08f00).
Not sure how the object is converted to NSData instead of NSDate.
来源:https://stackoverflow.com/questions/39515942/saving-nsdate-error-in-swift-3-and-ios-10