• 自定义类的归解档


    @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);

       

    }

  • 相关阅读:
    使用regsrv32.exe绕过应用程序白名单(多种方法)
    使用rundll32.exe绕过应用程序白名单(多种方法)
    Cobalt Strike 3.13的新功能
    kindeditor<=4.1.5上传漏洞复现
    Winrar目录穿越漏洞复现
    HTTP返回码总结
    如何将Debug文件夹下的资源打包成一个EXE文件直接执行
    我用VS2012在Nuget中安装Signalr之后报错
    System.Drawing.Color
    Asp.Net MVC及Web API框架配置会碰到的几个问题及解决方案
  • 原文地址:https://www.cnblogs.com/wujie123/p/5304582.html
Copyright © 2020-2023  润新知