NSObject + NSCoding Category
2015-12-31 19:32
501 查看
在.h文件中 <NSCoding>
在.m文件中
#import <objc/objc-runtime.h>
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [self
init];
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 = [NSString
stringWithCString: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 = [NSString
stringWithCString:propertyName
encoding:NSUTF8StringEncoding];
//
取出当前属性的值,相当于self.name
id value = [self
valueForKey:propertyNameStr];
[aCoder encodeObject:value
forKey:propertyNameStr];
}
free(propertyList);
}
在.m文件中
#import <objc/objc-runtime.h>
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [self
init];
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 = [NSString
stringWithCString: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 = [NSString
stringWithCString:propertyName
encoding:NSUTF8StringEncoding];
//
取出当前属性的值,相当于self.name
id value = [self
valueForKey:propertyNameStr];
[aCoder encodeObject:value
forKey:propertyNameStr];
}
free(propertyList);
}
相关文章推荐
- Objective-C之KVC、KVO
- Thread的join()方法和Object的wait(),notify(),notifyAll()方法
- Git CMD - show: Show various types of objects
- jdbcTemplate.queryForObject 方法注意
- Objective-C编码规范:26个方面解决iOS开发问题
- MyBatis中对List<Object> 对象List的批处理插入操作
- Objective-C 笔记 字符串操作
- Git CMD - fetch: Download objects and refs from another repository
- iOS 博客第一篇-深入理解Objective-c中@class的含义
- php的array object 互转
- Git CMD - push: Update remote refs along with associated objects
- Objective-C面试题和基本概念
- Objective-C关键字汇总和概念
- NSDictionary、NSMutableDictionary的基本用法
- NSMutableArray的使用... exchangeObjectAtIndex
- Objective-C Runtime
- C++中的函数对象(Function Object)
- 详解Objective-C的meta-class
- 详解Objective-C的meta-class
- IOS----Objective-C中JSON解析与XML解析