归档-ios

  /****普通对象归档**/

        NSString *homePath=NSHomeDirectory();

        NSString *fileName=@"test.vse";

        NSString *path=[homePath stringByAppendingPathComponent:fileName];

        NSArray *array=@[@"abc",@"sea",@123];

        BOOL success=[NSKeyedArchiver archiveRootObject:array toFile:path];

        if (success) {

            NSLog(@"success");

        }

        

        /**普通对象解归档***/

        NSArray *newArray=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

        NSLog(@"%@",newArray);

        

        /***第二种普通对象归档方式***/

        NSString *homePath2=NSHomeDirectory();

        NSString *fileName2=@"test2.vse";

        NSString *path2=[homePath2 stringByAppendingPathComponent:fileName2];

        NSMutableData *data=[NSMutableData data];

        NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

        NSArray *array2=@[@"abc222",@"se22a",@1233];

        [archiver encodeInt:100 forKey:@"age"];

        [archiver encodeObject:array2 forKey:@"name"];

        [archiver finishEncoding];

        BOOL success2=[data writeToFile:path2  atomically:YES];

        if (success2) {

            NSLog(@"success");

        }

        

        /***第二种普通对象解归档方式***/

        NSData *data2=[NSData dataWithContentsOfFile:path2];

        NSKeyedUnarchiver *unAchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:data2];

        int age=[unAchiver decodeIntForKey:@"age"];

        NSArray *name=[unAchiver decodeObjectForKey:@"name"];

        NSLog(@"age=%d,name=%@",age,name);

 //自定义对象归档

#import <Foundation/Foundation.h>

 @interface User : NSObject <NSCoding>

@property (nonatomic,copy) NSString *name;

@property (nonatomic,copy) NSString *email;

@property (nonatomic,copy) NSString *password;

@property (nonatomic,assign) int age;

 @end

------------------------------

#import "User.h"

#define AGE @"age"

#define NAME @"name"

#define EMAIL @"email"

#define PASSWORD @"password"

@implementation User:NSObject

//对属性编码,归档时调用

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

    [aCoder encodeInt:_age forKey:AGE];

    [aCoder encodeObject:_name  forKey:NAME];

    [aCoder encodeObject:_email forKey:EMAIL];

    [aCoder encodeObject:_password forKey:PASSWORD];

}

//对属性解码,解归档时调用

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

    self=[super init];

    if (self!=nil) {

      _age=[aDecoder decodeIntForKey:AGE];

      _name=[aDecoder decodeObjectForKey:NAME];

      _email=[aDecoder decodeObjectForKey:EMAIL];

      _password=[aDecoder decodeObjectForKey:PASSWORD];

    }

    return self;

}

//重写description

-(NSString *) description{

    NSString *str=[NSString stringWithFormat:@"age=%d,name=%@,email=%@,password=%@",_age,_name,_email,_password];

    return str;

}

 @end

----------------使用---------------------

     /***第三种自定义对象归档方式***/

        User *user=[[User alloc] init];

        user.name=@"caicai";

        user.age=22;

        user.email=@"adf@s.com";

        user.password=@"skjdsfsd";

        

        NSString *path3=[NSHomeDirectory() stringByAppendingPathComponent:@"user.hs"];

        BOOL success3=[NSKeyedArchiver archiveRootObject:user toFile:path3];

        if (success3) {

            NSLog(@"success");

        }

         /***自定义对象解归档方式***/

        User *user2=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

        NSLog(@"%@",user2);

原文地址:https://www.cnblogs.com/clarence/p/3916273.html