//归档 NSDictionary *aDic=[[NSDictionary alloc]initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3", nil]; NSArray *array=[[NSArray alloc]initWithObjects:@"One",@"Two",@"Three", nil]; NSMutableData *data1=[[NSMutableData alloc]init]; NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data1]; //将归档的数据全部写到data中,再将data中的数据写到文件中! [archiver encodeObject:aDic forKey:@"Mydic"];//将字典中的内容归档 [archiver encodeObject:array forKey:@"Myarray"];//将数组中的内容归档 [archiver finishEncoding]; [data1 writeToFile:PATH atomically:YES];//将归档后的数据统一写入文件中 [aDic release]; [array release]; [data1 release]; [archiver release]; //解归档 NSData *receiveData=[[NSData alloc]initWithContentsOfFile:PATH]; NSKeyedUnarchiver * unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:receiveData];// NSDictionary *unarchiverAdic=[unarchiver decodeObjectForKey:@"Mydic"]; NSLog(@"%@",unarchiverAdic); NSArray *unarchiverArray=[unarchiver decodeObjectForKey:@"Myarray"]; NSLog(@"%@",unarchiverArray); //以上是对OC中已有的数据类型进行的操作,若对自定义的数据类型进行归档与解归档,则方法如下 //1.头文件 #import <Foundation/Foundation.h> @interface Student : NSObject <NSCoding>{ NSString* _name; NSUInteger _age; //注:若该类是一个复合的数据类型,则该类中拥有的非系统自带的类型也必须实现NSCoding协议,否则无法归档,例如,假设该类含有一个Dog *_dog 的对象,该类非系统自带,则该类也必须实现NSCoding协议 } @property (copy,nonatomic) NSString* name; @property (assign,nonatomic) NSUInteger age; - (id)initWithName:(NSString*)aName andAge:(NSUInteger)aAge; //NSCoding协议中要求实现的方法 //- (void)encodeWithCoder:(NSCoder *)aCoder; //- (id)initWithCoder:(NSCoder *)aDecoder; @end //方法实现部分 #import "Student.h" @implementation Student - (id)init{ return [self initWithName:@"张三丰" andAge:100]; } - (NSString *)description { return [NSString stringWithFormat:@"Name:%@ Age:%lu", self.name,self.age]; } - (id)initWithName:(NSString*)aName andAge:(NSUInteger)aAge { if (self = [super init]) { _name = aName; _age = aAge; } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:[self name] forKey:@"Name"]; [aCoder encodeInteger:(NSUInteger)self.age forKey:@"Age"]; } - (id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super init]) { self.name = [aDecoder decodeObjectForKey:@"Name"]; self.age = (NSUInteger)[aDecoder decodeIntegerForKey:@"Age"]; } return self; } @end 3.main #import <Foundation/Foundation.h> #import "Student.h" int main(int argc, const char * argv[]) { @autoreleasepool { #if 0 //系统类型的归档和解档操作 NSDictionary* aDic = [[NSDictionary alloc]initWithObjectsAndKeys:@"hello",@"one",@"1000phone",@"two", nil]; NSString* path = @"/Users/youDirector/Desktop/aDic.archive"; //系统类的对象归档 [NSKeyedArchiver archiveRootObject:aDic toFile:path]; NSDictionary* bDic = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; NSLog(@"bDic:%@",bDic); NSLog(@"aDic:%@",aDic); #endif Student* aStu = [[Student alloc] init]; NSLog(@"aStu:%@",aStu); NSString* path = @"/Users/bencai/Desktop/aStu.archive"; [NSKeyedArchiver archiveRootObject:aStu toFile:path];//自定类归档 Student* bStu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];//解归档 NSLog(@"bStu:%@",bStu); } return 0; }
OC中归档与解归档
原创
©著作权归作者所有:来自51CTO博客作者NetworkAD的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
OC中的归档和反归档
所谓的归档和反归档也就是我们将数据存储到文件中,和从文件中获取数据,和字符串数组不
数据存储 归档 反归档 Objective-C hive -
分类与归档
侧边栏已经正确地显示了最新文章列表、归档、分类等信息。现在来完善归档和分类功能,当用户点击归档下的某个
hive html django