NSObject + NSCoding Category
来源:互联网 发布:网络歌手作曲 编辑:程序博客网 时间:2024/06/06 22:48
在.h文件中 <NSCoding>
在.m文件中
#import <objc/objc-runtime.h>
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [selfinit];
if (self) {
unsigned int count =0;
objc_property_t *propertyList = class_copyPropertyList([self class], &count);
for (int i =0; i < count; i++) {
objc_property_t property = propertyList[i];
const char *propertyName = property_getName(property);
// 将取出来的C语言字符串转化为OC字符串(char -> NSString)
NSString *propertyNameStr = [NSStringstringWithCString:propertyName encoding:NSUTF8StringEncoding];
// 解档取出属性的值
id value = [aDecoder decodeObjectForKey:propertyNameStr];
// 使用setValueForKey赋值,相当于set方法
[self setValue:value forKey:propertyNameStr];
}
free(propertyList);
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder {
unsigned int count =0;
objc_property_t *propertyList = class_copyPropertyList([self class], &count);
for (int i =0; i < count; i++) {
objc_property_t property = propertyList[i];
const char *propertyName =property_getName(property);
// 将取出来的C语言字符串转化为OC字符串(char -> NSString)
NSString *propertyNameStr = [NSStringstringWithCString:propertyName encoding:NSUTF8StringEncoding];
// 取出当前属性的值,相当于self.name
id value = [selfvalueForKey:propertyNameStr];
[aCoder encodeObject:value forKey:propertyNameStr];
}
free(propertyList);
}
- NSObject + NSCoding Category
- 对象的序列化NSObject<NSCoding>
- NSObject比较实用的Category
- NSCoding
- NSCoding
- NSCoding
- NSCoding
- NSCoding
- NSObject
- NSObject
- NSObject
- NSObject
- NSObject
- NSObject
- NSObject
- NSObject
- NSObject
- NSObject
- mycncart使用教程 - 如何修改网站后台用户名密码
- gregorian::date使用
- Hello Oninstack
- 如何为mycncart添加自定义链接
- Android 如何解决dialog弹出时无法捕捉Activity的back事件
- NSObject + NSCoding Category
- Maven设置代理服务器
- [Spring Boot 系列] 集成maven和Spring boot的profile功能
- 建站笔记
- 扩展GridView导出Excel功能
- java实现二叉树的建立以及前序中序后序遍历
- Java中三元运算符值得注意的地方
- View和XenDesktop,到底谁更简单 -- Part I
- MySQL执行计划解读