zoukankan      html  css  js  c++  java
  • 自定义类的归解档

    @interface Student : NSObject

    @property (strong,nonatomic) NSString *name;

    @property (assign,nonatomic) int age;

    @property (assign,nonatomic) char sex;

    -(Student *)initWithDictionary:(NSDictionary *)dic;

    @end

    @implementation Student

    -(Student *)initWithDictionary:(NSDictionary *)dic

    {

        self = [super init];

        if (self) {

            self.name = dic[@"name"];

            self.age = [dic[@"age"] intValue];

            self.sex = [dic[@"sex"] charValue];

        }

        return self;

    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

       

        NSDictionary *dic = @{@"name":@"lisi",@"age":@"30",@"sex":@'M'};

        Student *stu = [[Student alloc] initWithDictionary:dic];

        //归档

        NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"student.tt"];

        

        NSMutableData *data = [NSMutableData data];

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

        [keyArcher encodeObject:stu.name forKey:@"name"];

        [keyArcher encodeInt:stu.age forKey:@"age"];

        [keyArcher encodeInt:stu.sex forKey:@"sex"];

        

        [keyArcher finishEncoding];

        BOOL bol = [data writeToFile:path atomically:YES];

        NSLog(@"%d",bol);

        NSLog(@"%@",path);

        

        

        

        //解档

        

        NSData *data1 = [NSData dataWithContentsOfFile:path];

        NSKeyedUnarchiver *keyUn = [[NSKeyedUnarchiver alloc] initForReadingWithData:data1];

        

        NSString *name = [keyUn decodeObjectForKey:@"name"];

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

        char sex = [keyUn decodeIntForKey:@"sex"];

        

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

       

    }

  • 相关阅读:
    oracle 监听静态注册举例解析
    oracle监听动态注册与静态注册
    oracle startup mount nomount 区别
    RAC的时间同步问题
    RAC环境TNS-12541报错处理
    Oracle参数修改是否需要重启等
    面试提纲
    Dubbo是什么
    为什么要用dubbo,dubbo和zookeeper关系
    Java的参数传递是「按值传递」还是「按引用传递」?
  • 原文地址:https://www.cnblogs.com/wujie123/p/5304582.html
Copyright © 2011-2022 走看看