用runtime归档、解档、copy
来源:互联网 发布:centos安装vsftpd 编辑:程序博客网 时间:2024/06/06 21:02
1.我定义一个TestModel
//// TestModel.h// runTime//// Created by apple on 16/5/27.// Copyright © 2016年 李重阳. All rights reserved.//#import <Foundation/Foundation.h>#import <objc/runtime.h>@interface TestModel : NSObject<NSCoding,NSCopying>@property (nonatomic,assign) float height;@property (nonatomic,strong) NSArray * dataArr;@property (nonatomic,retain) NSArray * dataArr1;@property (nonatomic,copy) NSString * name;@property (nonatomic,retain) NSString * name2;- (instancetype)initWithDict:(NSDictionary *)dict;@end
//// TestModel.m// runTime//// Created by apple on 16/5/27.// Copyright © 2016年 李重阳. All rights reserved.//#import "TestModel.h"@implementation TestModel- (instancetype)initWithDict:(NSDictionary *)dict { if (self = [super init]) { //1.获取类的属性及属性对应的类型 NSMutableArray * keys = [NSMutableArray array]; NSMutableArray * attributes = [NSMutableArray array]; //获得底层的属性列表 unsigned int outCount = 0; objc_property_t *propertyList = class_copyPropertyList([self class], &outCount); for (int i = 0 ; i<outCount; i++) { objc_property_t property = propertyList[i]; const char *key = property_getName(property); const char *attribute = property_getAttributes(property); [keys addObject:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]]; [attributes addObject:[NSString stringWithCString:attribute encoding:NSUTF8StringEncoding]]; } free(propertyList); //通过keys 来赋值 for (NSString * key in keys) { if (dict[key]) { [self setValue:dict[key] forKey:key]; } } free(ivars); } return self;}//解档/* * 通过归档来初始化,也就是把这个归档来解出来 **/- (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { unsigned int outCount = 0; Ivar * ivars = class_copyIvarList([self class], &outCount); for (int i = 0; i<outCount; i++) { Ivar ivar = ivars[i]; NSString * key = [NSString stringWithUTF8String:ivar_getName(ivar)]; [self setValue:[aDecoder decodeObjectForKey:key] forKey:key]; } free(ivars); } return self;}/* * 归档 **/- (void)encodeWithCoder:(NSCoder *)aCoder { unsigned int outCount; Ivar * ivars = class_copyIvarList([self class], &outCount); for (int i = 0; i < outCount; i ++) { Ivar ivar = ivars[i]; NSString * key = [NSString stringWithUTF8String:ivar_getName(ivar)]; [aCoder encodeObject:[self valueForKey:key] forKey:key]; }}/*实现copy 协议**/- (id)copyWithZone:(NSZone *)zone { id copy = [[[self class]allocWithZone:zone]init]; unsigned int outCount; Ivar * ivars = class_copyIvarList([self class], &outCount); for (int i = 0; i < outCount; i ++) { Ivar ivar = ivars[i]; NSString * key = [NSString stringWithUTF8String:ivar_getName(ivar)]; id value = [self valueForKey:key]; [copy setValue:value forKey:key]; } free(ivars); return copy;}@end
0 0
- 用runtime归档、解档、copy
- runtime自动归档/解档
- iOS 对象的归档、解档 runtime
- Runtime的使用6 【归档、解档】
- runtime之归档反归档
- Runtime 学习-自动归档
- runtime 进行归档
- runtime使用篇----归档和反归档
- iOS runtime讲解,并且用runtime动态归档与解档
- 利用Runtime 实现自动化归档
- 利用Runtime 实现自动化归档
- 利用Runtime 实现自动化归档
- 利用Runtime实现自动化归档
- runtime 优化归档 ,替换方法
- 使用runtime归档模型对象
- runtime 实现 归档和解档
- runtime 进行归档和解档
- iOS 提升之利用runtime进行归档/解归档
- 猴子偷桃问题
- Markdown-入门指南及网站整合
- 将一个无符号十进制数的二进制逆转,然后以输出
- 不用(a+b)/2的方式求平均数
- 在eclipse用gdb调试stl容器配置
- 用runtime归档、解档、copy
- 输入字符串,统计其中数字,空格和其他字符的个数
- 一瓶汽水1元,两瓶汽水可换一瓶,现有20元,最多可喝多少瓶汽水
- 写一个函数可以左旋转字符串的k个字符
- 判断一个数是否在二维数组中
- 模拟实现strstr函数
- 实现n的k次方
- 输入一个数,最终将该数各个位数之和输出:for example: 1927 最终输出19
- android 获取sim卡运营商信息(转)