Reputation: 37581
I want to store an NSDateComponents to NSUserDefaults, so need to wrap it in an NSData first, how can this be done?
Alternatively is there another way of storing an NSDateComponent to user defaults?
Upvotes: 1
Views: 253
Reputation: 26907
NSDateComponents
conforms to NSCoding
protocol, so you can use NSKeyedArchiver
/NSKeyedUnarchiver
to do the job. Here is some sample code:
NSDateComponents *cmp = ...;
//Encoding
NSMutableData *data= [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:cmp];
[archiver finishEncoding];
//Decoding
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSDateComponents *decoded = [unarchiver decodeObject];
[unarchiver finishDecoding];
Upvotes: 3