时间:2021-05-02
前言
在iOS 中实现对象序列化,需要遵行NSCoding协议,然后对对象的每个属性进行归档和接档赋值,响应的操作比较繁琐。本文主要介绍 利用 runtime遍历属性 大大简化代码量,下面来看看详细的介绍吧。
具体实现代码如下:
1.先建立NSobject的分类, 定义可能用到的相关类型
? 1 2 3 4 5 6 7 8 9 10 11 static NSString *intType = @"i"; // int_32t(枚举int型) static NSString *longTpye = @"l"; //long类型 static NSString *longlongType= @"q"; // longlong类型 static NSString *BoolType = @"B"; //bool类型 static NSString *floatType = @"f"; // float static NSString *doubleType = @"d"; // double static NSString *boolType = @"c"; static NSString *stringType = @"NSString"; // NSString 类型 static NSString *numberType = @"NSNumber"; // NSNumber 类型 static NSString *arrayType = @"arrayType";//array类型 static NSString *imageType = @"UIImage"; // UIImage 类型然后在归档方法中便利自身的属性名称,并且取出自身属性对应的值,进行存储到本地。此时遍历类属性本身,用到了Ivar指针(定义对象的实例变量,包括类型和名字),具体代码如下
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 //归档 - (void)encodeWithCoder:(NSCoder *)aCoder { unsigned int count; // 属性个数 Ivar *varArray = class_copyIvarList([self class], &count); for (int i = 0; i < count; i++) { Ivar var = varArray[i]; const char *cName = ivar_getName(var); // 属性名c字符串 NSString *proName = [[NSString stringWithUTF8String:cName] substringFromIndex:1]; //OC字符串,并且去掉下划线 _ const char *cType = ivar_getTypeEncoding(var); // 获取变量类型,c字符串 id value = [self valueForKey:proName]; NSString *proType = [NSString stringWithUTF8String:cType]; // oc 字符串 if ([proType containsString:@"NSString"]) { proType = stringType; } if ([proType containsString:@"NSNumber"]) { proType = numberType; } if ([proType containsString:@"NSArray"]) { proType = arrayType; } if ([proType containsString:@"UIImage"]) { proType = imageType; } // (5). 根据类型进行编码 if ([proType isEqualToString:intType] || [proType isEqualToString:boolType] || [proType isEqualToString:BoolType]) { [aCoder encodeInt32:[value intValue] forKey:proName]; } else if ([proType isEqualToString:longTpye]) { [aCoder encodeInt64:[value longValue] forKey:proName]; } else if ([proType isEqualToString:floatType]) { [aCoder encodeFloat:[value floatValue] forKey:proName]; } else if ([proType isEqualToString:longlongType] || [proType isEqualToString:doubleType]) { [aCoder encodeDouble:[value doubleValue] forKey:proName]; } else if ([proType isEqualToString:stringType]) { // string 类型 [aCoder encodeObject:value forKey:proName]; } else if ([proType isEqualToString:numberType]) { [aCoder encodeObject:value forKey:proName]; } else if ([proType isEqualToString:arrayType]) { [aCoder encodeObject:value forKey:proName]; } else if ([proType isEqualToString:imageType]) { // image 类型 [aCoder encodeDataObject:UIImagePNGRepresentation(value)]; } } free(varArray); }其次进行解档, 原理和归档差不多, 直接上代码
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 - (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [self init]; if (self) { unsigned int count; Ivar *varArray = class_copyIvarList([self class], &count); for (int i = 0; i < count; i++) { Ivar var = varArray[i]; const char *cName = ivar_getName(var); // 属性名c字符串 NSString *proName = [[NSString stringWithUTF8String:cName] substringFromIndex:1]; //OC字符串,并且去掉下划线 _ const char *cType = ivar_getTypeEncoding(var); // 获取变量类型,c字符串 NSString *proType = [NSString stringWithUTF8String:cType]; // oc 字符串 if ([proType containsString:@"NSString"]) { proType = stringType; } if ([proType containsString:@"NSNumber"]) { proType = numberType; } if ([proType containsString:@"NSArray"]) { proType = arrayType; } if ([proType containsString:@"UIImage"]) { proType = imageType; } if ([proType isEqualToString:intType] || [proType isEqualToString:boolType] || [proType isEqualToString:BoolType]) { int32_t number = [aDecoder decodeInt32ForKey:proName]; [self setValue:@(number) forKey:proName]; } else if ([proType isEqualToString:longTpye]) { int64_t number = [aDecoder decodeInt64ForKey:proName]; [self setValue:@(number) forKey:proName]; } else if ([proType isEqualToString:floatType]) { float number = [aDecoder decodeFloatForKey:proName]; [self setValue:@(number) forKey:proName]; } else if ([proType isEqualToString:longlongType] || [proType isEqualToString:doubleType]) { double number = [aDecoder decodeFloatForKey:proName]; [self setValue:@(number) forKey:proName]; } else if ([proType isEqualToString:stringType]) { // string 类型 NSString *string = [aDecoder decodeObjectForKey:proName]; [self setValue:string forKey:proName]; } else if ([proType isEqualToString:numberType]) { NSString *number = [aDecoder decodeObjectForKey:proName]; [self setValue:number forKey:proName]; } else if ([proType isEqualToString:arrayType]) { NSArray *array = [aDecoder decodeObjectForKey:proName]; [self setValue:array forKey:proName]; } else if ([proType isEqualToString:imageType]) { // image 类型 UIImage *image = [UIImage imageWithData:[aDecoder decodeDataObject]]; [self setValue:image forKey:proName]; } } } return self; }最后也就是 存储方法 、 清除存储的本地缓存 和 获取本地存储数据的方法
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 //存储路径 - (NSString *)filePathWithUniqueFlagString:(NSString *)uniqueFlag { NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *detailPath = [NSString stringWithFormat:@"%@_%@",uniqueFlag,[NSString stringWithUTF8String:object_getClassName(self)]]; NSString *path = [docPath stringByAppendingPathComponent:detailPath]; return path; } //保存对象数据到本地 - (void)saveDataToLocalWithUniqueFlagKey:(NSString *)uniqueFlagKey { [NSKeyedArchiver archiveRootObject:self toFile:[self filePathWithUniqueFlagString:uniqueFlagKey]]; } //清空本地存储的对象数据 - (id)getDataFromLocalWithUniqueFlagKey:(NSString *)uniqueFlagKey { return [NSKeyedUnarchiver unarchiveObjectWithFile:[self filePathWithUniqueFlagString:uniqueFlagKey]]; } //从本地获取对象数据 - (BOOL)removeDataFromLocalWithUniqueFlagKey:(NSString *)uniqueFlagKey { NSError *error = nil; [[NSFileManager defaultManager] removeItemAtPath:[self filePathWithUniqueFlagString:uniqueFlagKey] error:&error]; if (!error) { return YES; } else { return NO; } }完整项目下载地址如下:https://github.com/maxzhang123/MXCoding.git
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:http://www.jianshu.com/p/0384fcf6ca95
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
详解Python序列化Serialize和反序列化Deserialize序列化(serialization)序列化是将对象状态转换为可保持或传输的格式的过程。与
Java序列化和反序列化实例详解在分布式应用中,对象只有经过序列化才能在各个分布式组件之间传输,这就涉及到两个方面的技术-发送者将对象序列化,接受者将对象反序列
序列化与反序列化是.net程序设计中常见的应用,本文即以实例展示了.net实现序列化与反序列化的方法。具体如下:一般来说,.net中的序列化其实就是将一个对象的
Java—IO流对象的序列化和反序列化序列化的基本操作 1.对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化。 2.序列化流(Obje
一、picklepickle模块用来实现python对象的序列化和反序列化。通常地pickle将python对象序列化为二进制流或文件。python对象与文件之