runtime&归档解档
demo:
HDYUserInfo.h
#import <Foundation/Foundation.h>
@interface HDYUserInfo :NSObject<NSCoding>
@property(nonatomic,copy)NSString*UserAccessToken;
@property(nonatomic,copy)NSString*UserAccessSecret;
@property(nonatomic,assign)NSIntegerUserId;
@property(nonatomic,copy)NSString*UserName;
@property(nonatomic,copy)NSString*NickName;
@property(nonatomic,copy)NSString*HeadPortraitUrl;
@property(nonatomic,copy)NSString*Signature;
@property(nonatomic,copy)NSString*BlackgroundPic;
@property(nonatomic,assign)NSIntegerisVerification;
@property(nonatomic,assign)NSIntegerisReplyJoin;
@property(nonatomic,assign)NSIntegerisSearch;
@property(nonatomic,copy)NSString*Language;
@property(nonatomic,assign)NSIntegerFontSizeValue;
@property(nonatomic,assign)NSIntegerisRecommand;
@property(nonatomic,assign)NSIntegersex;
//新消息提醒
@property(nonatomic,assign)NSIntegerisVoice;
@property(nonatomic,assign)NSIntegerisVibrate;
@property(nonatomic,assign)NSIntegerisMessage;
//隐私
@property(nonatomic,assign)NSIntegerisSearchNumber;
@property(nonatomic,assign)NSIntegerisAddFriends;
@property(nonatomic,assign)NSIntegerisContact;
@end
------------------------------------------------------------
HDYUserInfo.m
#import"HDYUserInfo.h"
@implementationHDYUserInfo
- (void)encodeWithCoder:(NSCoder*)aCoder
{
unsigned int count =0;
Ivar*ivars =class_copyIvarList([HDYUserInfo class], &count);
for(inti =0; i < count; i ++) {
//取出i位置对应的成员变量
Ivar ivar = ivars[i];
//查看成员变量
constchar*name =ivar_getName(ivar);
//归档
NSString*key = [NSString stringWithUTF8String:name];
id value = [self valueForKey:key];
[aCoder encodeObject:value forKey:key];
}
//释放内存
free(ivars);
}
- (id)initWithCoder:(NSCoder*)aDecoder
{
if(self= [super init])
{
unsigned int count =0;
Ivar*ivars =class_copyIvarList([HDYUserInfo class], &count);
for(inti =0; i < count; i ++) {
//取出i位置对应的成员变量
Ivar ivar = ivars[i];
//查看成员变量
constchar*name =ivar_getName(ivar);
//归档
NSString*key = [NSString stringWithUTF8String:name];
id value = [aDecoder decodeObjectForKey:key];
//设置到成员变量身上
[self setValue:value forKey:key];
}
free(ivars);
}
returnself;
}
@end
----------------------------邪恶的分界线--------------------------------
PS:
都说给10K以下就一个一个写,给10~15K用for循环,给15K以上用runtime。。。哈哈,也就那么一说,工作还是要认真的对待,即使你现在拿着一个一个写的工资却干着Runtime的活儿,相信我最终受益的还是你!