runtime 实现nscoding

来源:互联网 发布:json 对日期格式化 编辑:程序博客网 时间:2024/06/09 14:26

最近闲来无事,偶然想到以前面试提到的一个问题,NSCoding协议,有点复杂,如果属性少还好,如果100多个属性,则会显得麻烦。

特意写了一个,进行了测试,测试OK。

代码如下:

1、Person.h

#import <Foundation/Foundation.h>


@interface Person : NSObject<NSCoding>

@property(nonatomic,strong)NSString * name;//名字

@property(nonatomic,strong)NSString * sex;//性别

@property(nonatomic,strong)NSString * old;//年龄

@property(nonatomic,strong)NSString * address;//地址

-(instancetype)initWithName:(NSString *)name sex:(NSString *)sex old:(NSString *)old address:(NSString *)address;

@end


Person.m

#import "Person.h"

#import <objc/runtime.h>

@implementation Person

-(instancetype)initWithName:(NSString *)name sex:(NSString *)sex old:(NSString *)old address:(NSString *)address

{

    if (self = [superinit]) {

        _name = name;

        _sex = sex;

        _old = old;

        _address = address;

    }

    return self;

}

-(NSString *)description

{

    return  [NSStringstringWithFormat:@"name:%@  sex:%@  age:%@  address:%@",self.name,self.sex,self.old,self.address];

}

/**常规解档归档*/

-(void)encodeWithCoder:(NSCoder *)aCoder

{

    [aCoder encodeObject:_nameforKey:@"name"];

    [aCoder encodeObject:_sexforKey:@"sex"];

    [aCoder encodeObject:_oldforKey:@"old"];

    [aCoder encodeObject:_addressforKey:@"adress"];

}

-(instancetype)initWithCoder:(NSCoder *)aDecoder

{

    if (self = [superinit]) {

        _name = [aDecoderdecodeObjectForKey:@"name"];

        _sex = [aDecoderdecodeObjectForKey:@"sex"];

        _old = [aDecoderdecodeObjectForKey:@"old"];

        _address = [aDecoderdecodeObjectForKey:@"adress"];

    }

    return self;

}

//runtime写法

-(void)encodeWithCoder:(NSCoder *)aCoder

{

    unsignedint count =0;

    Ivar * ivarList =class_copyIvarList([Personclass], &count);

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

        constchar * name =ivar_getName(ivarList[i]);

        NSString * strname = [NSStringstringWithUTF8String:name];

        [aCoder encodeObject:[selfvalueForKey:strname]forKey:strname];

    }

    free(ivarList);

}

-(instancetype)initWithCoder:(NSCoder *)aDecoder

{

    if (self = [superinit]) {

        unsignedint count =0;

        Ivar * ivarList =class_copyIvarList([Personclass], &count);

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

            constchar * name =ivar_getName(ivarList[i]);

            NSString * strname = [NSStringstringWithUTF8String:name];

            id value = [aDecoderdecodeObjectForKey:strname];

            [selfsetValue:valueforKey:strname];

        }

        free(ivarList);

    }

    returnself;

}

2、然后再创建一个学生类Student,继承Person类

Student.h

#import <Foundation/Foundation.h>

#import "Person.h"

@interface Student : Person

@property(nonatomic,strong)NSString * course;//课程

-(instancetype)initWithName:(NSString *)name sex:(NSString *)sex old:(NSString *)old address:(NSString *)address course:(NSString *)course;

@end

Student.m

#import "Student.h"


@implementation Student

-(instancetype)initWithName:(NSString *)name sex:(NSString *)sex old:(NSString *)old address:(NSString *)address course:(NSString *)course

{

    if (self = [superinitWithName:namesex:sexold:oldaddress:address]) {

        _course = course;

    }

    returnself;

}


-(NSString *)description

{

    return  [NSStringstringWithFormat:@"name:%@  sex:%@  age:%@  address:%@ course:%@",self.name,self.sex,self.old,self.address,_course];

}

//常规归档接档

-(instancetype)initWithCoder:(NSCoder *)aDecoder

{

    if (self = [superinitWithCoder:aDecoder]) {

        _course = [aDecoderdecodeObjectForKey:@"course"];

    }

    returnself;

}

-(void)encodeWithCoder:(NSCoder *)aCoder

{

    [superencodeWithCoder:aCoder];

    [aCoder encodeObject:_courseforKey:@"course"];

}

@end

3、实现接档 归档

Student * stu1 = [[Studentalloc]initWithName:@"狗娃"sex:@"男"old:@"5"address:@"深圳市"course:@"美术"];

    Student * stu2 = [[Studentalloc]initWithName:@"狗剩"sex:@"男"old:@"50"address:@"深圳市"course:@"美术"];

    Student * stu3 = [[Studentalloc]initWithName:@"狗仔"sex:@"男"old:@"15"address:@"深圳市"course:@"美术"];

    NSMutableData  * data = [NSMutableDatadata];

    NSKeyedArchiver * archive = [[NSKeyedArchiveralloc]initForWritingWithMutableData:data];

    [archive encodeObject:stu1forKey:@"stu1"];

    [archive encodeObject:stu2forKey:@"stu2"];

    [archive encodeObject:stu3forKey:@"stu3"];

    [archive finishEncoding];

    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObjectstringByAppendingString:@"stu"];

    [data writeToFile:pathatomically:YES];

    

    NSData * data1 = [NSDatadataWithContentsOfFile:path];

    NSKeyedUnarchiver * unarchive = [[NSKeyedUnarchiveralloc]initForReadingWithData:data1];

    Student * ss1 = [unarchivedecodeObjectForKey:@"stu1"];

    Student * ss2 = [unarchivedecodeObjectForKey:@"stu2"];

    Student * ss3 = [unarchivedecodeObjectForKey:@"stu3"];

    [unarchive finishDecoding];

    

    NSLog(@"\nstu1:%@\nstu2:%@\nstu3:%@\nss1:%@\nss2:%@\nss3:%@\n",stu1,stu2,stu3,ss1,ss2,ss3);

打印结果:

2017-02-22 15:35:40.941 runtime---解档归档[4059:1262700] 

stu1:name:狗娃  sex:男  age:5  address:深圳市 course:美术

stu2:name:狗剩  sex:男  age:50  address:深圳市 course:美术

stu3:name:狗仔  sex:男  age:15  address:深圳市 course:美术

ss1:name:狗娃  sex:男  age:5  address:深圳市 course:美术

ss2:name:狗剩  sex:男  age:50  address:深圳市 course:美术

ss3:name:狗仔  sex:男  age:15  address:深圳市 course:美术



参考“http://www.jianshu.com/p/25a319aee33d


0 0
原创粉丝点击