IOS:OC--归档与反归档

1.新建类 “Person ”
2.Person .h
@interface Person : NSObject
/*
1.首先:复杂对象所属的类必须遵循《NSCoding》协议

序列化和反序列化
2.- (void)encodeWithCoder:(NSCoder *)aCoder;
序列化---归档

  • (nullable instancetype)initWithCoder:(NSCoder *)aDecoder;
    反序列化---反归档
    */
    @property(nonatomic,strong)NSString *name;
    @property(nonatomic,strong)NSString *gender;
    @property(nonatomic,assign)int age;
    3.Person .m
    //归档
    //对person对象进行归档时,此方法执行
    //对person中想要进行归档时的所有属性,进行序列化操作

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

    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInteger:self.age forKey:@"age"];
    }
    //反归档
    //对person进行反归档时该方法执行
    //创建一个新的person对象,所有属性通过反序列化的到

  • (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]){
    self.name = [aDecoder decodeObjectForKey:@"name"];
    self.age=[aDecoder decodeIntForKey:@"age"];
    }
    return self;
    }
    4.ViewController.m

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //归档:将对象类型转换成数据类型,以文件的形式存储,如果想要读取归档中的数据,可以通过反归档形式,还原数据。
    /归档三种形式:
    1、将Foundation框架中对象进行归档
    2、对自定义内容进行归档
    3、自定义类对象进行归档
    /

// [self archiver];
//[self writeUserDef];
[self Archiver];

}

-(void)Archiver{

//获取document文件路径
NSString * document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSLog(@"%@",document);
//在document文件夹。创建新的文件
NSString * filePath = [document stringByAppendingPathComponent:@"customFile"];
//1.使用data对象进行归档
NSMutableData * archiverdata = [NSMutableData data ];
//2.创建归档对象
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:archiverdata];
//3.归档数据
[archiver encodeObject:@"小花" forKey:@"xm"];
[archiver encodeObject:@[@"华晨宇",@"吴亦凡 ",@"彭于晏"] forKey:@"name"];
[archiver encodeInt:28 forKey:@"age"];
//4.完成归档
[archiver finishEncoding];
//5.导入
BOOL result = [archiverdata writeToFile:filePath atomically:YES];

if (result) {
    NSLog(@"归档成功%@",filePath);
}
else{
    NSLog(@"归档失败");
}

//反归档
//拿到数据文件
NSData * unarchiverData = [NSData dataWithContentsOfFile:filePath];
//进行反归档
//1.创建反归档对象
NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:unarchiverData];
NSString * strData = [unarchiver decodeObjectForKey:@"xm"];
NSLog(@"%@,%@",strData,filePath);

}

pragma mark ---NSUserDefaults

-(void)writeUserDef{
/*
NSUserDefaults是一个单例类。只有一个实例,可用于数据的永久存储
可以存储 NSNumber NSString NSData NSArray
*/
NSArray * array=@[@"宋仲基",@"华晨宇",@"吴亦凡"];
NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
//存储
[user setObject:array forKey:@"userArray"];
//取
NSArray * arr = [user objectForKey:@"userArray"];
NSLog(@"%@",arr);

#warning 注意:要保证key的唯一,不唯一就会被覆盖:对象(必须是不可变)
//NSUserdefaults:存储对象不可变,如果存储可变,首先要将可变,转为不可变
NSMutableArray * mutableArray=[NSMutableArray arrayWithObjects:@"133",@"456",@"789",nil];
//将可变,转为不可变
NSArray * arr1=[NSArray arrayWithArray:mutableArray];
//存储
NSUserDefaults * user2 = [NSUserDefaults standardUserDefaults];
[user2 setObject:array forKey:@"存放的一定是不可变的"];
//自定义数据类型
Person * per = [Person new];
per.name=@"彭于晏";
per.gender=@"男";
per.age=18;
//存放person的数组
NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:0];
//将person转化为data类型  整体
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:per];
//将data 写入到数组中
NSUserDefaults * user3 = [NSUserDefaults standardUserDefaults];
[user3 setObject:data forKey:@"person"];
NSLog(@"%@",user3);
//取出数据
NSUserDefaults * user4=[NSUserDefaults standardUserDefaults];
NSData * data1 = [user4 objectForKey:@"person"];
Person * per1= [NSKeyedUnarchiver unarchiveObjectWithData:data1];
NSLog(@"%@",per1);

}

pragma mark --- 复杂对象写入文件

-(void)archiver{
Person * person=[Person new];
person.name = @"华晨宇";
person.age=18;
person.gender=@"男";

//准备路径
NSString * path=NSHomeDirectory();
NSLog(@"%@",path);

path=[path stringByAppendingPathComponent:@"singDog.txt"];
//1.准备存储数据对象
NSMutableData *data=[NSMutableData data];
//2创建归档对象
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//3开始归档
[archiver encodeObject:person forKey:@"person"];
//4完成归档
[archiver finishEncoding];
//5.写入到文件当中
BOOL result=[data writeToFile:path atomically:YES];
if (result){
    NSLog(@"归档成功%@",path);
}
else{
    NSLog(@"归档失败");
}

pragma mark --- 反归档/反序列化/解码/解档

//准备解档路径
NSData * mydata = [NSData dataWithContentsOfFile:path];
//创建反归档对象
NSKeyedUnarchiver * unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:mydata ];
//反归档
Person * per = [Person new];
per = [unarchiver decodeObjectForKey:@"person"];
//完成反归档
[unarchiver finishDecoding];
//测试
NSLog(@"%@",per.name);

}

你可能感兴趣的:(IOS:OC--归档与反归档)