1. 最基本的归档
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject]stringByAppendingPathComponent:@"test.txt"];
// 这个地方可以是任意的类型
NSArray *arr = [NSArrayarrayWithObjects:@"hhahahah",@"hhahahah",@"hhahahah", nil];
[NSKeyedArchiver archiveRootObject:arrtoFile:path];
NSArray *hh = [NSKeyedUnarchiverunarchiveObjectWithFile:path];
2. 模型归档解档
//解档
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [superinit])
{
self.access_token = [aDecoderdecodeObjectForKey:@"access_token"];
self.gender = [aDecoderdecodeObjectForKey:@"gender"];
self.location = [aDecoderdecodeObjectForKey:@"location"];
self.openid = [aDecoderdecodeObjectForKey:@"openid"];
self.uid = [aDecoderdecodeObjectForKey:@"uid"];
self.screen_name = [aDecoderdecodeObjectForKey:@"screen_name"];
self.profile_image_url = [aDecoderdecodeObjectForKey:@"profile_image_url"];
self.Phone = [aDecoderdecodeObjectForKey:@"Phone"];
self.Email = [aDecoderdecodeObjectForKey:@"Email"];
}
return self;
}
//归档
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.access_tokenforKey:@"access_token"];
[aCoder encodeObject:self.genderforKey:@"gender"];
[aCoder encodeObject:self.locationforKey:@"location"];
[aCoder encodeObject:self.openidforKey:@"openid"];
[aCoder encodeObject:self.uidforKey:@"uid"];
[aCoder encodeObject:self.screen_nameforKey:@"screen_name"];
[aCoder encodeObject:self.profile_image_urlforKey:@"profile_image_url"];
[aCoder encodeObject:self.PhoneforKey:@"Phone"];
[aCoder encodeObject:self.EmailforKey:@"Email"];
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
NSLog(@"%s, %@", __func__, key);
}
Model *model = [[Modelalloc]init];
model.uid = 12;
model.ID = 22.2;
model.createTime = @"dddddd";
model.nickname = @"rdfvdfvc";
model.descriptionText =@"hgfdsr";
model.iconUrl = @"hgfdvxz";
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject]stringByAppendingPathComponent:@"test.txt"];
[NSKeyedArchiver archiveRootObject:modeltoFile:path];
Model *model111 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
3. 模型继承模型的归档
本类集成上面的Model模型 进行归档
@interface hhModel : Model
@property (nonatomic,copy) NSString *createModel;// 提问的时间
@end
-(void)encodeWithCoder:(NSCoder *)aCoder {
[super encodeWithCoder:aCoder];
[aCoder encodeObject:self.createModelforKey:@"createModel"];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [superinitWithCoder:aDecoder];
if (self) {
self.createModel = [aDecoderdecodeObjectForKey:@"createModel"];
}
return self;
}
hhModel *model = [[hhModelalloc] init];
model.uid = 12;
model.ID = 22.2;
model.createTime = @"dddddd";
model.nickname = @"rdfvdfvc";
model.descriptionText =@"hgfdsr";
model.iconUrl = @"hgfdvxz";
model.createModel =@"ffffffffffff";
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"test.txt"];
[NSKeyedArchiverarchiveRootObject:model toFile:path];
hhModel *model111 = [NSKeyedUnarchiverunarchiveObjectWithFile:path];