OC多个对象的归档和解归档

FJStudent.h

#import <Foundation/Foundation.h>


@interface FJStudent : NSObject <NSCoding>


@property (nonatomic,copy) NSString *name;


@property (nonatomic,assign) int score;




@end


FJStudent.m

#import "FJStudent.h"


@implementation FJStudent

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

    

    [aCoder encodeObject:_name forKey:@"name"];

    

    [aCoder encodeInt:_score forKey:@"score"];

    

}



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

    

    if (self = [super init]) {

        _name = [aDecoder decodeObjectForKey:@"name"];

        _score = [aDecoder decodeIntForKey:@"score"];

    }

    return self;

}



- (NSString *)description{

    return [NSString stringWithFormat:@"%@ %d",_name,_score];

}


@end


main.m

#import <Foundation/Foundation.h>

#import "FJStudent.h"

#define path @"/Users/IOS1601/Desktop/plist文件/student"

void achiver();

void unAchiver();

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        

        achiver();

        unAchiver();

    }

    return 0;

}

#pragma mark -归档

void achiver(){

    

    NSMutableArray *studentArray = [[NSMutableArray alloc]init];

    

    for (int i=0; i<20; i++) {

        

        FJStudent *stu = [[FJStudent alloc] init];

        

        stu.name = [NSString stringWithFormat:@"name%d",i];

        

        stu.score = arc4random() % 56 + 15;

       

        [studentArray addObject:stu];

        

    }

    

//    //=======归档过程1=========

//    //1.创建一个可变的mdata

//    NSMutableData *mdata = [[NSMutableData alloc]init];

//    

//    //2.创建一个归档工具

//    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]

//                                 initForWritingWithMutableData:mdata];

//    

//    //3.归档,将数组进行归档

//    [archiver encodeObject:studentArray forKey:@"array"];

//    

//    //4.关闭归档

//    [archiver finishEncoding];

//    

//    //5.写入本地文件

//    [mdata writeToFile:path atomically:NO];

    

    //=============归档过程2============

    //参数一:需要归档的对象

    //参数二:归档的路径

    [NSKeyedArchiver archiveRootObject:studentArray toFile:path];

    

}

#pragma mark -解归档


void unAchiver(){

//    //=========解归档的过程1=============

//    //1.创建一个NSData

//    NSData *data = [[NSData alloc]initWithContentsOfFile:path];

//    

//    //2.创建一个解归档工具

//    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]

//                                     initForReadingWithData:data];

//    

//    //3.解归档

//    NSArray * studentArray =  [unarchiver

//                               decodeObjectForKey:@"array"];

//    

//    //4.关闭归档工具

//    [unarchiver finishDecoding];

//    

//    NSLog(@"%@",studentArray);

    

    //=========解归档的过程2=============

    //参数:需要解归档文件的路径;

   NSArray *studentArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];


    NSLog(@"%@",studentArray);

}




转载于:https://my.oschina.net/luhoney/blog/650095

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值