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
- runtime 实现nscoding
- 使用runtime实现NSCoding协议
- NSCoding 协议 父类只需要实现一次,所有子类 都可以 继承 的 runtime特性
- NSCoding 协议 父类只需要实现一次,所有子类 都可以 继承 的 runtime特性
- Runtime之NSCoding的自动归档、接档
- NSCoding
- NSCoding
- NSCoding
- NSCoding
- NSCoding
- iOS nscoding 实现归档解档
- RunTime实现
- NSCoding协议
- NSCoding解释
- NSCoding协议
- NSCoding / NSKeyedArchiver
- NSCoding协议
- NSCoding协议
- 市场需求分析(MRD)模板
- ArcGIS Engine中如何往已有要素类中插入数据
- css梯形、六角星、五角星、六边形
- 默认情况下从svn checkout项目报错解决方法
- 解读Netty之接收缓冲区
- runtime 实现nscoding
- Linux命令行
- ?c#把一行数据插入到datatable里面去?
- TOKEN+签名验证
- 寒假第一次录一个小小的模拟慕课视频吧~<关于函数定义和调用>
- ubuntu windows远程桌面连接xrdp相关问题
- select的if选择
- 【工作笔记】互斥锁
- Android 5.0共享元素学习总结