// // 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