문제

I've looked at the timeIntervalSinceReferenceDate function of NSDate. Can I use this function to store the interval to disk and then return it to NSDate with the same value as the original? I'm wary that the reference or interval could vary between machines and come up differently on another computer?

도움이 되었습니까?

해결책

NSDate can be archived as an NSData instance and NSData can be easily written to / read from disk.

// Create and store it
NSDate * date = [NSDate date];
NSData * dateData = [NSKeyedArchiver archivedDataWithRootObject:date];
[dateData writeToFile:@"/Some/path/to/file.dat" atomically:NO];

// Now bring it back
NSData * restoredDateData = [NSData dataWithContentsOfFile:@"/Some/path/to/file.dat"];
NSDate * restoredDate = [NSKeyedUnarchiver unarchiveObjectWithData:restoredDateData];

No error checking is done. Do better than that. ;-)

다른 팁

Alternatively, if you want to store the result of timeIntervalSinceReferenceDate you can store it in an NSNumber as a double and then save that to disk using Joshua's NSKeyedArchiver method.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top