数据序列化需要当前类遵循NSCoding
协议,该协议有两个方法需要实现
- (instancetype)initWithCoder:(NSCoder *)aDecoder;
//反序列化数据
- (void)encodeWithCoder:(NSCoder *)aCoder;
//序列化数据
举例:
@interface Person : NSObject<NSCoding>
@property (nonatomic, copy)NSString *name;
@property (nonatomic, assign)NSInteger age;
@end
@implementation Person
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
@end
调用:
//序列化存储数据
Person *p = [[Person alloc]init];
p.name = @"keyedArchiver";
p.age = 30;
NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"personData"];
[NSKeyedArchiver archiveRootObject:p toFile:file];
//取数据反序列化
NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"personData"];
Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
if (p) {
NSLog(@"p.name %@ p.age %ld",p.name,p.age);
}
如果一个类的属性比较多,请使用
runtime
来获取类的属性,这样就比较方便。请看示例代码:
#import "Person.h"
@interface Student : Person<NSCoding>
@property (nonatomic, strong)NSString *school;
@end
#import <objc/runtime.h>
@implementation Student
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
unsigned int count = 0;
objc_property_t *propertys = class_copyPropertyList(self.class, &count);
//class_copyPropertyList只能获取到当前类的所有属性,不能获取到父类的属性,所以要调用super initWithCoder.如果想获取类的所有属性和所有成员变量,请使用
//Ivar *vars = class_copyIvarList(sel.class, &count);
//Ivar var = vars[0];
// const char *name = ivar_getName(ivar);
// NSString *key = [NSString stringWithUTF8String:name];
for (NSInteger i = 0 ; i < count; i++) {
objc_property_t property = propertys[i];
const char *propertyNameChar = property_getName(property);
NSString *propertyNameStr = [NSString stringWithUTF8String:propertyNameChar];
id value = [aDecoder decodeObjectForKey:propertyNameStr];
[self setValue:value forKey:propertyNameStr];//kvc
}
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[super encodeWithCoder:aCoder];
unsigned int count = 0;
objc_property_t *propertys = class_copyPropertyList(self.class, &count);
for (NSInteger i = 0 ; i < count; i++) {
objc_property_t property = propertys[i];
const char *propertyNameChar = property_getName(property);
NSString *propertyNameStr = [NSString stringWithUTF8String:propertyNameChar];
id value = [self valueForKey:propertyNameStr];//kvc
[aCoder encodeObject:value forKey:propertyNameStr];
}
}
@end
注意点:
如果当前类继承的父类也是有属性的,在序列化与反序列化时需要这样调用
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[super encodeWithCoder:aCoder];
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
网友评论