标签:
1、NSKeyedArchiver
// 得到沙盒路径
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
// 拼接整个文件路径
NSString *file = [document stringByAppendingString:@"userDate"];
// 进行对象的归档
[NSKeyedArchiver archiveRootObject:account toFile:file];
到这里还不够,还有说明哪些属性要归档,哪些属性将来要解党。
首先需要遵守 NSCoding 协议。
@interface IWAccount : NSObject <NSCoding>
/**
* 从文件中读取对象的时候调用
*
*/
- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init])
{
// 这里注意
self.access_token = [decoder decodeObjectForKey:@"access_token"];
self.expires_in = [decoder decodeInt64ForKey:@"expires_in"];
self.remind_in = [decoder decodeInt64ForKey:@"remind_in"];
self.uid = [decoder decodeInt64ForKey:@"uid"];
self.expiresTime = [decoder decodeObjectForKey:@"expiresTime"];
}
return self;
}
/**
* 将对象写入文件的时候调用
*
* @param enaCoder
*/
- (void)encodeWithCoder:(NSCoder *)enaCoder
{
[enaCoder encodeObject:self.access_token forKey:@"access_token"];
[enaCoder encodeInt64:self.expires_in forKey:@"expires_in"];
[enaCoder encodeInt64:self.remind_in forKey:@"remind_in"];
[enaCoder encodeInt64:self.uid forKey:@"uid"];
[enaCoder encodeObject:self.expiresTime forKey:@"expiresTime"];
}
标签:
原文地址:http://www.cnblogs.com/cjt5132/p/4769927.html