#import <Foundation/Foundation.h>框架
@interface PYPeople : NSObject函数
@property (nonatomic, assign) int age;学习
@property (nonatomic, assign) int height;atom
@property (nonatomic, copy) NSString *name;code
@property (nonatomic, assign) int age2;对象
@property (nonatomic, assign) int height2;get
@property (nonatomic, assign) int age3;string
@property (nonatomic, assign) int height3;it
@property (nonatomic, assign) int age4;io
@property (nonatomic, assign) int height4;
@end
#import "PYPeople.h"
#import <objc/runtime.h>
@implementation PYPeople
-(void)encodeWithCoder:(NSCoder *)encoder{
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([PYPeople class], &count);
for (int i=0; i<count; 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([PYPeople 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;
}
@end
runtime机制首先要了解下面几个问题 1相关的头文件和函数 1> 头文件
<objc/runtime.h>
2> 相关应用
3> 相关函数
4.必备常识 1> Ivar : 成员变量 2> Method : 成员方法 从上面例子中咱们看到咱们定义的成员变量,若是要是动态建立方法,可使用Method,