// // Person.m // 自动归档 // 这是一个类文件中的实现,能够将类目提取出去,,这个文件没有问题 // Created by 刘磊磊 on 15/10/14. // Copyright © 2015年 iOS. All rights reserved. // #import "Person.h" #import <objc/runtime.h> @interface NSObject (getProperties) - (NSDictionary *)properties_aps; @end @implementation NSObject(getProperties) //属性名和属性值(引用某位大神的获取属性值的代码,再次敬礼) - (NSDictionary *)properties_aps { NSMutableDictionary *props = [NSMutableDictionary dictionary]; unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([self class], &outCount); for (i = 0; i<outCount; i++) { objc_property_t property = properties[i]; const char* char_f =property_getName(property); NSString *propertyName = [NSString stringWithUTF8String:char_f]; id propertyValue = [self valueForKey:(NSString *)propertyName]; if (propertyValue) [props setObject:propertyValue forKey:propertyName]; } free(properties); return props; } - (NSArray *)getAllProperties { u_int count; objc_property_t *properties =class_copyPropertyList([self class], &count); NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count]; for (int i = 0; i<count; i++) { const char* propertyName =property_getName(properties[i]); [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]]; } free(properties); return propertiesArray; } @end @implementation Person - (void)printProperty{ NSLog(@"%@",[self properties_aps]); } - (instancetype)initWithCoder:(NSCoder *)aDecoder{ if (self = [super init]) { for (NSString* pro in [self properties_aps]) { [self setValue:[aDecoder decodeObjectForKey:pro] forKey:pro]; } } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder{ NSDictionary *all = [self properties_aps]; for (NSString *key in all.keyEnumerator) { [aCoder encodeObject:all[key] forKey:key]; } } @end
下面是将归档和反归档封装了一下数组
// // LLLArchive.m // 自动归档 // // Created by 刘磊磊 on 15/10/14. // Copyright © 2015年 iOS. All rights reserved. // #import "LLLArchive.h" @implementation LLLArchive //归档是否成功 传入一个对象 以及保存的路径(需加文件名)//将对象加入到数组当中 -(BOOL)archiverWithObject:(NSDictionary *)objectsAndName andSavePath:(NSString *)path{ NSMutableData *data = [NSMutableData data]; for (NSString *key in objectsAndName.keyEnumerator) { [self.archiver encodeObject:objectsAndName[key] forKey:key]; } [self.archiver finishEncoding]; return [data writeToFile:path atomically:YES]; } @end @interface LLLUnArchive: NSObject @end @implementation LLLUnArchive //反归档 dic返回的名称对应的对象 path读取的路径 对象的名称数组 类名 -(BOOL)unArchiverWithObject:(void(^)(NSMutableDictionary * dic))classBlock andSourcePath:(NSString *)path andObjectsName:(NSArray *)names andClassName:(NSString *)className{ NSMutableDictionary *dicc = [NSMutableDictionary dictionary]; NSData *data = [NSData dataWithContentsOfFile:path]; NSKeyedUnarchiver* unArchive = [[NSKeyedUnarchiver alloc]initForReadingWithData:data]; for (NSString *string in names) { Class clazz = [NSClassFromString(className) alloc]init]; acb = [unArchive decodeObjectForKey:string]; [dicc setValue:acb forKey:string]; } classBlock(dicc); if (!dicc) { return NO; } return YES; } @end
但愿你们可以留言,尽快完善,谢谢atom