zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

数据存储之归档

存储数据 归档
2023-09-14 08:57:58 时间
在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦; 偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置信息) 归档:因为前两者都有一个致命的缺陷,只能存储常用的类型。归档可以实现把自定义的对象存放在文件中。 先看下项目的结构  Student类继承Person类 在ViewController.m中
//

// ViewController.m

// Archive

// Created by City--Online on 15/4/22.

// Copyright (c) 2015年 CYW. All rights reserved.

#import "ViewController.h"

#import "Student.h"

@interface ViewController ()

@implementation ViewController

- (void)viewDidLoad {

 [super viewDidLoad];

 NSArray *documents=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

 NSString *documentpath=[documents firstObject];

// NSLog(@"%@",documentpath);

 NSString *filePath=[documentpath stringByAppendingPathComponent:@"CYW"];

//使用archiveRootObject简单归档 单个对象

 //缺点:只能把一个对象归档到一个文件

#if 0

 NSArray *arr=[[NSArray alloc]initWithObjects:@"1",@"2",@"3", nil];

 //归档 //可对字典、数组、字符串、数字等进行归档 返回bool值表示是否归档成功

 [NSKeyedArchiver archiveRootObject:arr toFile:filePath];

#endif

#if 0

 //解挡

 NSArray *arr=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

 NSLog(@"%@",arr);

#endif

 //多个对象归档

#if 0

 //准备数据

 CGPoint point=CGPointMake(100, 100);

 NSString *mystring=@"cuiyanwei";

 BOOL YesOrNo=YES;

 //归档

 NSMutableData *mutableData=[[NSMutableData alloc]init];

 NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData];

 [archiver encodeCGPoint:point forKey:@"mypoint"];

 [archiver encodeBool:YesOrNo forKey:@"mybool"];

 [archiver encodeObject:mystring forKey:@"mystring"];

 [archiver finishEncoding];

 [mutableData writeToFile:filePath atomically:YES];

#endif

#if 0

 //多对象解档

 NSMutableData *mutableData=[[NSMutableData alloc]initWithContentsOfFile:filePath];

 NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:mutableData];

 CGPoint point=[unarchiver decodeCGPointForKey:@"mypoint"];

 BOOL mybool=[unarchiver decodeBoolForKey:@"mybool"];

 NSString *mystring=[unarchiver decodeObjectForKey:@"mystring"];

 [unarchiver finishDecoding];

 NSLog(@"X=%lf,Y=%lf\n mybool=%d\n mystring=%@",point.x,point.y,mybool,mystring);

 UIImage

#endif

#if 0

 //自定义对象归档

 Student *stu1=[[Student alloc]init];

 stu1.name=@"cuiyanwei";

 stu1.age=24;

 stu1.photo=[UIImage imageNamed:@"email.png"];

 stu1.stuId=@"001";

 Student *stu2=[[Student alloc]init];

 stu2.name=@"xiaocui";

 stu2.age=23;

 stu2.photo=[UIImage imageNamed:@"email.png"];

 stu2.stuId=@"002";

 //归档

 NSArray *array=[[NSArray alloc]initWithObjects:stu1,stu2, nil];

 NSMutableData *mutableData=[[NSMutableData alloc]init];

 NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData];

 [archiver encodeObject:array forKey:@"students"];

 [archiver finishEncoding];

 [mutableData writeToFile:filePath atomically:YES];

#endif

#if 1

 //解档

 NSMutableData *mutableData=[[NSMutableData alloc]initWithContentsOfFile:filePath];

 NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:mutableData];

 NSArray *array=[unarchiver decodeObjectForKey:@"students"];

 for (Student *stu in array) {

 NSLog(@"name=%@ age=%ld photo=%@ stuId=%@",stu.name,stu.age,stu.photo,stu.stuId);

#endif

- (void)didReceiveMemoryWarning {

 [super didReceiveMemoryWarning];

 // Dispose of any resources that can be recreated.

@end
在Person.h中定义了几个属性

[aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeInteger:self.age forKey:@"age"]; [aCoder encodeObject:self.photo forKey:@"photo"]; - (id)initWithCoder:(NSCoder *)aDecoder if (self=[super init]) { NSLog(@"person encodeWithCoder"); _age=[aDecoder decodeIntegerForKey:@"age"]; _name=[aDecoder decodeObjectForKey:@"name"]; _photo=[aDecoder decodeObjectForKey:@"photo"]; return self; @end  在Student类中继承Person类 增加了一个学号属性

// Created by City--Online on 15/4/22. // Copyright (c) 2015年 CYW. All rights reserved. #import "Student.h" @implementation Student - (void)encodeWithCoder:(NSCoder *)aCoder [super encodeWithCoder:aCoder]; NSLog(@"student encodeWithCoder"); [aCoder encodeObject:self.stuId forKey:@"stuId"]; - (id)initWithCoder:(NSCoder *)aDecoder if (self=[super initWithCoder:aDecoder]) { NSLog(@"student initWithCoder"); _stuId=[aDecoder decodeObjectForKey:@"stuId"]; return self; @end

 自定义模型对象归档解档运行结果如下:

主要是实现NSCoding协议


储留香:天庭需要怎样的数据存储系统 在中国神话故事中,高达九重的天庭之中神仙众多,二十八星宿、三十六天罡、七十二地煞等等,每位大仙也多是长生不死,少则几百、多则几万岁,要保障如此庞大的系统组织高效运转就需要一个良好的数据存储系统。
2、设置归档存放路径(只可以设置其一,不可以并行) a) log_archive_dest:指定归档文件存放的路径,该路径只能是本地磁盘,默认为 ,不能与以下参数同使用。
社会主义 从.Net到iOS,在撸的道路上越走越远,工作之余经营着博客园http://www.cnblogs.com/5ishare,欢迎小伙伴(妹子更好)一起交流,谈谈人生理想。作为经常从网上索取免费资料的一员,要有回报回报的思想,也让更多的人少走弯路.