2012-07-19 76 views

回答

3

NSDateComponents符合NSCoding協議,所以您可以使用NSKeyedArchiver/NSKeyedUnarchiver來完成這項工作。下面是一些示例代碼:

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]; 
+1

這可以用更少的代碼使用類方法'archivedDataWithRootObject來完成:'和'unarchiveObjectWithData:'。 – omz 2012-07-19 22:12:32

相關問題