今天的项目中由于后台返回的数据有点别致,8800多条数据存放在一个数组中.所以我在前端处理数据,并存到了一个数组和三个字典中.为了提高界面交互体验,决定使用归档反归档本地化这些数组和字典.
由于数组和字典中最终存放的是自定义的model,所以model必须遵循归档协议和拷贝协议----
在PhoneModel.h中
@interface PhoneModel : NSObject<NSCoding, NSCopying>
在PhoneModel.m中实现相应的协议方法
#pragma mark NSCoding
-(void)encodeWithCoder:(NSCoder *)aCoder{//编码
[aCoder encodeObject:_class_layer forKey:kclass_layerKey];
[aCoder encodeObject:_ID forKey:kIDKey];
[aCoder encodeObject:_parent_id forKey:kparent_idKey];
[aCoder encodeObject:_sort_id forKey:ksort_idKey];
[aCoder encodeObject:_title forKey:ktitleKey];
}
-(id)initWithCoder:(NSCoder *)aDecoder{//解码
if(self == [super init]){
_class_layer = [aDecoder decodeObjectForKey:kclass_layerKey];
_ID = [aDecoder decodeObjectForKey:kIDKey];
_parent_id = [aDecoder decodeObjectForKey:kparent_idKey];
_sort_id = [aDecoder decodeObjectForKey:ksort_idKey];
_title = [aDecoder decodeObjectForKey:ktitleKey];
}
return self;
}
#pragma mark -
#pragma mark NSCopying
-(id)copyWithZone:(NSZone *)zone{
PhoneModel *model = [[[self class]allocWithZone:zone]init];
model.class_layer = [self.class_layer copyWithZone:zone];
model.ID = [self.ID copyWithZone:zone];
model.parent_id = [self.parent_id copyWithZone:zone];
model.sort_id = [self.sort_id copyWithZone:zone];
model.title = [self.title copyWithZone:zone];
return model;
}
在viewController.m中
#define kPinPaiFileName @"pinpaiPath"
#define kXingHaoFileName @"xinghaoPath.data"
#define kPinPaiDataKey @"pinpaiData"
#define kXingHaoDataKey @"xinghaoData"
//1.本地品牌数据的路径
-(NSString *)pinpaiFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kPinPaiFileName];
}
//2.本地型号数据的路径
-(NSString *)xinghaoFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kXingHaoFileName];
}
数组和字典归档--
//1.保存品牌数据源
NSMutableData *pinpaiData = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:pinpaiData];
[archiver encodeObject:self.pinpaiArray forKey:kPinPaiDataKey];
[archiver finishEncoding];
[pinpaiData writeToFile:[self pinpaiFilePath] atomically:YES];
//2.保存型号数据源
NSMutableData *xinghaoData = [[NSMutableData alloc] init];
NSKeyedArchiver *xinghaoArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:xinghaoData];
[xinghaoArchiver encodeObject:self.xinghaoDic forKey:kXingHaoDataKey];
[xinghaoArchiver finishEncoding];
[xinghaoData writeToFile:[self xinghaoFilePath] atomically:YES];
数组和字典反归档--
//1.取品牌数据
NSLog(@"路径:%@", [self pinpaiFilePath]);
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self pinpaiFilePath]];
NSKeyedUnarchiver *unarchiver = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];//此处一定要用alloc,不能用横线划掉的方法,否则取出来的数据个数为零.
NSMutableArray *array = [unarchiver decodeObjectForKey:kPinPaiDataKey];
[unarchiver finishDecoding];
//2.取型号数据
NSData *xinghaoData = [[NSMutableData alloc] initWithContentsOfFile:[self xinghaoFilePath]];
NSKeyedUnarchiver *xinghaoUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:xinghaoData];
NSMutableDictionary *xinghaoDic = [xinghaoUnarchiver decodeObjectForKey:kXingHaoDataKey];
[xinghaoUnarchiver finishDecoding];
网友评论