ios:NSKeyedArchiver/NSKeyedUnarchiver(归档/解档)

我去昨天写的归解档怎么没传上去 郁闷 今天来补。

1.NSKeyedArchiver(归档)是一种系统工具

它的具体功能是把遵守NSCoding协议的复杂类对象编码后放到一块NSMutableData里。然后我们把NSMutableData写入文件,完成数据的存储。

ios:NSKeyedArchiver/NSKeyedUnarchiver(归档/解档)_第1张图片

注意只有遵守NSCoding协议的类的对象才能归档,否则会crush、

#import 

@interface person : NSObject 

@property (nonatomic, copy) NSString* name;
@property (nonatomic, copy) NSString* phone;

- (void) encodeWithCoder:(NSCoder *)aCoder;

-(id)initWithCoder:(NSCoder *)aDecoder;

@end


  

   //编码 (序列化)
- (void) encodeWithCoder:(NSCoder *)aCoder{
    // 类里面有多少个属性,就需要对多少个属性进行编码
    [aCoder encodeObject:self.name forKey:@"pName"];
    [aCoder encodeObject:self.phone forKey:@"pPhone"];
}
    //反编码(反序列化)
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self= [super init];
    if (self) {
    //把编号的数据 反编码到本身属性上
        self.name = [aDecoder decodeObjectForKey: @"pName"];
        self.phone = [aDecoder decodeObjectForKey: @"pPhone"];
    }
    return self;
}
然后进行归档

  //复杂对象写入本地化(归解档)
    //复杂对象
    //归档7步
    //1. 有一个遵守了NSCoding协议的复杂对象
    //2.初始化对象
    //3.创建NSMutabelData
    //4. 使用NSMData初始化归档工具
    //5.通过工具将对象编码到NSMData
    //6. 结束编码
    //7. 把NSData写到本地

   person *personn = [[person alloc]init];
    personn.name = @"张三";
    personn.phone = @"13210001200";
    NSMutableData *personData = [NSMutableData data];

    //创建归档工具(把归档工具和一块数据区连接起来)
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:personData];
    
 //将person类 编码到persondate里面
    [archiver encodeObject:personn forKey:@"person"];
    //结束编码
       [archiver finishEncoding];
    NSString *path1 = [documentStr stringByAppendingString:@"/ person.text"];
    [personData writeToFile:path1 atomically:YES];

下面是解档,可以理解为归档的逆程序

 //解档
  //  1.从本地取出归档时的NSData文档
  //  2.用取出来得数据区初始接档工具
  //  3.把反编码的数据负到本身属性上
   // 4.通过初始化一个person类,通过key值接受解档后的数据
   // 5.结束反编码
    NSData *data2 = [NSData dataWithContentsOfFile:path1];
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data2];
    person* per = [unarchiver decodeObjectForKey:@"person"];
    [unarchiver finishDecoding];

好了下面介绍 更为简单地归档解档代码

   //解档
  person * person1 = [[person alloc]init];
   person1.name = @"吕";
    person1.phone = @"222222";
    //归档到path路径下的文件
   [NSKeyedArchiver archiveRootObject:person1 toFile:path];
    //从path解档出来
    person* perNew = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

归档数组

先把复杂类对象放到数组里在归档数组。

- (void)complexObjectArchiver
{
    Person *personA = [[Person alloc] init];
    personA.name = @"张三";
    personA.age = 20;
    personA.sex = @"男";
    
    Person *personB = [[Person alloc] init];
    personB.name = @"李四";
    personB.age = 15;
    personB.sex = @"女";
    
    Person *personC = [[Person alloc] init];
    personC.name = @"王五";
    personC.age = 30;
    personC.sex = @"男";
    
    NSString *paths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSLog(@"Caches: %@", paths);
    
    NSMutableArray *personsArr = [NSMutableArray arrayWithObjects:personA, personB, personC, nil];
    NSString *personsArrPath = [paths stringByAppendingString:@"/personsArr.plist"];
    [NSKeyedArchiver archiveRootObject:personsArr toFile:personsArrPath];
    NSLog(@"personsArrPath: %@", personsArrPath);
    
    NSArray *newPersonsArr = [NSKeyedUnarchiver unarchiveObjectWithFile:personsArrPath];
    
    NSLog(@"反归档: %@", newPersonsArr);
    
    for (Person *tempPerson in newPersonsArr) {
        NSLog(@"name: %@, age: %ld, sex: %@", tempPerson.name, tempPerson.age, tempPerson.sex);
    }


你可能感兴趣的:(网络编程)