runtime学习实战一:类的属性进行归档解档
#import <Foundation/Foundation.h>
@interface PYPerson : NSObject
@property (nonatomic, assign) int age;
@property (nonatomic, assign) int height;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age2;
@property (nonatomic, assign) int height2;
@property (nonatomic, assign) int age3;
@property (nonatomic, assign) int height3;
@property (nonatomic, assign) int age4;
@property (nonatomic, assign) int height4;
@end
#import "PYPerson.h"
#import <objc/runtime.h>
@implementation PYPerson
- (instancetype)init {
if (self = [super init]) {
_age = 1;
_height = 2;
_name = @"name";
_age2 = 3;
_height2 = 4;
_age3 = 5;
_height3 = 6;
_age4 = 7;
_height4 = 8;
}
return self;
}
- (NSString *)description {
return @"我是一个实例person";
}
- (void)encodeWithCoder:(NSCoder *)encoder {
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([PYPerson class], &count);
for (int i = 0; i<count; i++) {
// 取出i位置对应的成员变量
Ivar ivar = ivars[i];
// 查看成员变量
const char *name = ivar_getName(ivar);
// 归档
NSString *key = [NSString stringWithUTF8String:name];
id value = [self valueForKey:key];
[encoder encodeObject:value forKey:key];
}
free(ivars);
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([PYPerson class], &count);
for (int i = 0; i<count; i++) {
// 取出i位置对应的成员变量
Ivar ivar = ivars[i];
// 查看成员变量
const char *name = ivar_getName(ivar);
// 归档
NSString *key = [NSString stringWithUTF8String:name];
id value = [decoder decodeObjectForKey:key];
// 设置到成员变量身上
[self setValue:value forKey:key];
}
free(ivars);
}
return self;
}
#import <Foundation/Foundation.h>
#import "PYPerson.h"
#import "APLProduct.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
PYPerson *person = [[PYPerson alloc] init];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:person] forKey:@"person"];
NSData *personData = [[NSUserDefaults standardUserDefaults] objectForKey:@"person"];
if (personData != nil) {
PYPerson *tmpPerson = [NSKeyedUnarchiver unarchiveObjectWithData:personData];
NSLog(@"%@", tmpPerson);
}
}
return 0;
}
runtime学习实战一:类的属性进行归档解档的更多相关文章
- Delphi中TStringList类常用属性方法详解
TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 先把要讨论的几个属性列出来: 1.CommaText 2.Delim ...
- iOS开发中的4种数据持久化方式【一、属性列表与归档解档】
iOS中的永久存储,也就是在关机重新启动设备,或者关闭应用时,不会丢失数据.在实际开发应用时,往往需要持久存储数据的,这样用户才能在对应用进行操作后,再次启动能看到自己更改的结果与痕迹.ios开发中, ...
- 利用Runtime对Ivar实例变量进行共用的归档和解档方式
一.介绍 在OC中每一个对象持有的变量都是实例变量,实例变量包括成员变量和属性变量,在runtime中用Ivar表示对象的实例变量.其实,runtime源码中可以看到,Ivar也是一个结构体(基本上在 ...
- ios开发runtime学习四:动态添加属性
#import "ViewController.h" #import "Person.h" #import "NSObject+Property.h& ...
- 【delphi】TStringList类常用属性方法详解
TStringList 常用方法与属性 var List: TStringList; i: Integer; begin List := TStringList.Create; List.Add('S ...
- 用runtime来重写Coder和deCode方法 归档解档的时候使用
当我们归档自定义对象的时候,可以重写自定义Model的的encodeWithCoder和initWithCoder 开始的大概是这样的,当属性非常多的时候 这种方式就会觉得不还好 好像重复在做一样的事 ...
- iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值
在上篇博客<iOS开发之使用Runtime给Model类赋值>中介绍了如何使用运行时在实体类的基类中添加给实体类的属性赋值的方法,这个方法的前提是字典的Key必须和实体类的Property ...
- Runtime应用(三)实现NSCoding的自动归档和自动解档
当我们需要将一个对象进行归档时,都要让该对象的类遵守NSCoding协议,再实现归档和接档方法.例如有一个Person类,该类有两个成员变量 @property (nonatomic,copy) NS ...
- ibernate学习笔记5---实体类或属性名与数据库关键字冲突、hql命名参数、hql实现通用分页
一.实体类或属性名与数据库关键字冲突问题1.实体类名与数据库中的关键字冲突比如:实体表User与oracle中的系统表冲突解决方式1:在xml中添加table属性,指定表名,使其不与name默认相等 ...
随机推荐
- spring-task
cronExpression的配置说明,具体使用以及参数请百度google 字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小 ...
- 深入学习 celery
一.amqp交换 参考链接: http://www.cnblogs.com/ajianbeyourself/p/4950758.html
- strcpy strlen memcpy等的函数实现
#include <assert.h> #include <string.h> #include <stdlib.h> #include <stdio.h&g ...
- Git和.gitignore
http://blog.csdn.net/cscmaker/article/details/8553980 在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .giti ...
- hibernate一级缓存的源码初窥
hibernate的一级缓存的存在使得hibernate可以在操作实体化对象的时候减少对于数据库的访问.hibernate的一级缓存实际上就是指的session缓存,它的生命周期和session相同. ...
- raw转qcow2
raw转qcow2命令 : qemu-img convert -f raw test.img -O qcow2 test.img.qcow2 转晚之后,还需要virsh edit test,修改里面的 ...
- jQuery 表格删除,添加行
var colsNum = 4; 1,$(document),ready(function () { $.("#id1").parent().after('<tr class ...
- 设置DataSource后DateGridView不显示的问题
在一个WinForm小程序中,有两处需要用DataGridView控件显示数据.设置DataGridView.DataSource为数据查询结果后,第一个DataGridView可以正常显示数据,而第 ...
- Apache Commons Collections
http://commons.apache.org/proper/commons-collections/userguide.html 1. Utilities SetUtils Collection ...
- jq变态全选vs原生变态全选
<script> $(function(){ var num=0; $("#btn").on('click',function(){ if(this.checked){ ...