一杠园、資料歸檔篇
創(chuàng)建TLPeople.h
#import <Foundation/Foundation.h>
@interfaceTLPeople :NSObject
@property(nonatomic,copy)NSString*name;//姓名
@property(nonatomic,strong)NSNumber*age;//年齡
@property(nonatomic,copy)NSString*occupation;//職業(yè)
@property(nonatomic,copy)NSString*nationality;//國籍
@end
TLPeople.m
#import"TLPeople.h"
#if TARGET_IPHONE_SIMULATOR
#import <objc/objc-runtime.h>
#else
#import <objc/runtime.h>
#import <objc/message.h>
#endif
@implementationTLPeople
-(void)encodeWithCoder:(NSCoder*)aCoder
{
unsignedintcount =0;
Ivar*ivars =class_copyIvarList([TLPeopleclass], &count);
for(NSUIntegeri =0; i < count; i ++) {
Ivarivar = ivars[i];
constchar*name =ivar_getName(ivar);
NSString*key = [NSStringstringWithUTF8String:name];
idvalue = [selfvalueForKey:key];
[aCoderencodeObject:valueforKey:key];
}
free(ivars);
}
-(instancetype)initWithCoder:(NSCoder*)aDecoder
{
self= [superinit];
if(self) {
unsignedintcount =0;
Ivar*ivars =class_copyIvarList([TLPeopleclass], &count);
for(NSUIntegeri =0; i < count; i ++) {
Ivarivar = ivars[i];
constchar*name =ivar_getName(ivar);
NSString*key = [NSStringstringWithUTF8String:name];
idvalue = [aDecoderdecodeObjectForKey:key];
[selfsetValue:valueforKey:key];
}
free(ivars);
}
returnself;
}
@end
Demo傳送門-> 6.1 資料歸檔篇Demo