#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,