#import <Foundation/Foundation.h>

@interface Student : NSObject <NSCoding>;
@property(nonatomic,strong)NSString* name;
@property(nonatomic,assign)int age;
@property(nonatomic,copy)NSString* tel; -(id)initWithName:(NSString*)name endAge:(int)age endTel:(NSString*)tel; @end #import "Student.h" @implementation Student
@synthesize name=_name,tel=_tel,age=_age;
-(id)initWithName:(NSString *)name endAge:(int)age endTel:(NSString *)tel
{
if (self=[super init]) {
self.name=name;
self.age=age;
self.tel=tel;
}
return self;
} -(void)encodeWithCoder:(NSCoder *)aCoder
{
NSLog(@"编码中。。。。");
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInt:self.age forKey:@"age"];
[aCoder encodeObject:self.tel forKey:@"tel"];
// [aCoder encodeValueOfObjCType:@"" at:<#(const void *)#>] }
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"解码中。。。。");
self=[self initWithName:[aDecoder decodeObjectForKey:@"name"] endAge:[aDecoder decodeIntForKey:@"age"] endTel:[aDecoder decodeObjectForKey:@"tel"]]; return self;
} @end #import "ViewController.h"
#import "Student.h"
@interface ViewController ()
{
UITextField* _tf1;
UITextField* _tf2;
UITextField* _tf3;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self encodeStu]; // Do any additional setup after loading the view, typically from a nib.
// Dispose of any resources that can be recreated
_tf1=[[UITextField alloc]initWithFrame:CGRectMake(, , , )]; _tf2=[[UITextField alloc]initWithFrame:CGRectMake(, , , )];
_tf2.keyboardType=UIKeyboardTypeDecimalPad; _tf3=[[UITextField alloc]initWithFrame:CGRectMake(, , , )]; _tf1.borderStyle=UITextBorderStyleRoundedRect;
_tf2.borderStyle=UITextBorderStyleRoundedRect;
_tf3.borderStyle=UITextBorderStyleRoundedRect;
[self.view addSubview:_tf1];
[self.view addSubview:_tf2];
[self.view addSubview:_tf3]; //进入后台,触发写文件方法,
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(write) name:UIApplicationDidEnterBackgroundNotification object:nil ];
//进入应用时触发
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reade) name:UIApplicationDidBecomeActiveNotification object:nil ]; NSDictionary* dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"name",@"xxx",@"age",@"",@"tel",@"", nil];
NSLog(@"%@",[dic objectForKey:@""]);
}
-(void)reade{
//读取文件路径
NSString * path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:]stringByAppendingPathComponent:@"writefile.text" ];
NSLog(@"read path :%@",path);
NSArray* arr=[NSArray arrayWithContentsOfFile:path];
_tf1.text=arr[];
_tf2.text=arr[];
_tf3.text=arr[]; } -(void)viewWillAppear:(BOOL)animated
{ } -(void)write{
NSString * path=[[NSBundle mainBundle]bundlePath]; //path是文件夹,那么就再加上文件名才是文件完整路径
NSString* path2=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:];
// path2 = [path stringByAppendingString:@"/writefiles.tet"];
path2= [path2 stringByAppendingPathComponent:@"writefile.text"]; NSLog(@"%@,%@",path,path2);
NSArray* writeArr=@[_tf1.text,_tf2.text,_tf3.text]; [writeArr writeToFile:path2 atomically:YES]; } //归档解决自定义对象
//数据持久化本质:保持状态跟数据 //归档调用,加密写入文件
-(void)loadView
{
[super loadView]; //从程序输入数据 并构建自定义对象(初始化要归档的对象,是数据的生成) Student* stu=[[Student alloc]initWithName:@"啊城" endAge: endTel:@""];
// NSArray* stu=[[NSArray alloc]initWithObjects:@"sss",@"aaaa",@"222", nil];
NSMutableData* data=[[NSMutableData alloc]init];
//构建归档器
NSKeyedArchiver* archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//归档 没有储存动作,只负责编码
[archiver encodeObject:stu forKey:@"stu"];
//千万不能忘记,归档完毕马上调用
[archiver finishEncoding]; //写入文档
[data writeToFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:]stringByAppendingPathComponent:@"bba1.text" ] atomically:YES];
NSLog(@"111 Stu name is %@,gender is %d,tel is %@",stu.name,stu.age,stu.tel);
}
//解档
-(void)encodeStu{
//取数据,以待解码归档,注意一定要用NSDocumentDirectory
NSData* data= [NSData dataWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:]stringByAppendingPathComponent:@"bba1.text" ]];
//解档器
NSKeyedUnarchiver* uA=[[NSKeyedUnarchiver alloc]initForReadingWithData:data];
//解档->还原对象,注意解档和键需要和归档一致
Student* stu=[uA decodeObjectForKey:@"stu"];
//解码器,中decodeObjectForKay方法,在解码过程中,会调用initWithCoder方法,并通过对象类型到类中调用已重写实现的initWithCoder方法 NSLog(@"2222 Stu name is %@,gender is %d,tel is %@",stu.name,stu.age,stu.tel);
//
[uA finishDecoding]; } -(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; } @end

IOS s数据存储之归档解档的更多相关文章

  1. IOS数据存储之归档/解档

    前言: 前天学习了NSUserDefaults,我们知道NSUserDefaults不能保存自定义对象,所以我们今天来认识一下归档(NSKeyedArchiver)和解档(NSKeyedUnarchi ...

  2. 数据存储之归档解档 NSKeyedArchiver NSKeyedUnarchiver

    在构建应用程序时,有一个重要的问题是如何在每次启动之间持久化数据,以便重现最后一次关闭应用前的状态.在iOS和OS X上,苹果提供了三种选择:Core Data.属性列表(Property List) ...

  3. iOS开发UI篇—ios应用数据存储方式(归档)

    iOS开发UI篇—ios应用数据存储方式(归档)  一.简单说明 在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦: 偏好设置(将所有的东西都保存在同 ...

  4. iOS开发UI篇—ios应用数据存储方式(归档) :转发

    本文转发至:文顶顶http://www.cnblogs.com/wendingding/p/3775293.html iOS开发UI篇—ios应用数据存储方式(归档)  一.简单说明 在使用plist ...

  5. ios应用数据存储方式(归档) - 转

    一.简单说明  1.在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦.  2.偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置 ...

  6. 数据持久化------Archiving(归档,解档)

    其中TRPerson为自定义的继承自NSObject的类的子类  其中有两个属性,name 和 age .h文件 #import @interface TRPerson : NSObject<& ...

  7. iOS开发——UI进阶篇(十一)应用沙盒,归档,解档,偏好设置,plist存储,NSData,自定义对象归档解档

    1.iOS应用数据存储的常用方式XML属性列表(plist)归档Preference(偏好设置)NSKeyedArchiver归档(NSCoding)SQLite3 Core Data 2.应用沙盒每 ...

  8. iOS开发中的4种数据持久化方式【一、属性列表与归档解档】

    iOS中的永久存储,也就是在关机重新启动设备,或者关闭应用时,不会丢失数据.在实际开发应用时,往往需要持久存储数据的,这样用户才能在对应用进行操作后,再次启动能看到自己更改的结果与痕迹.ios开发中, ...

  9. iOS 浅复制、深复制、完全复制的知识点梳理验证(附加归档解档)

    在之前转载的一片文章中,文中对浅复制和深复制进行了详细的解读,同时还提到了深复制(one-level-deep copy).完全复制(true copy)的概念,并指出iOS开发中的深复制是单层深赋值 ...

随机推荐

  1. linux 文档处理命令

    1. 将用户信息数据库文件和组信息数据纵向合并为一个文件/1.txt(覆盖) 2.将用户信息数据库文件和用户密码数据库文件纵向合并为一个文件/2.txt(追加 3.将/1.txt./2.txt两个文件 ...

  2. C++中引用的本质

    一般的教材上讲到引用时,都是说"引用是对象的一个别名".我认为这种定义是不清晰的,不利于初学者理解引用.至少我自己曾经被这个定义困扰了一段时间.到底什么是"别名" ...

  3. 发送SMS短信(JSON) 转载

    http://blog.csdn.net/ldl22847/article/details/42553883 public   static string GetMobileConfByUserId( ...

  4. php源码之计算两个文件的相对路径

    <?php //计算出两个文件的相对路径即path2相对于$path1的相对路径 // http://www.manongjc.com/article/1342.html function ge ...

  5. 用wireshark抓包分析TCP三次握手、四次挥手以及TCP实现可靠传输的机制

    关于TCP三次握手和四次挥手大家都在<计算机网络>课程里学过,还记得当时高超老师耐心地讲解.大学里我遇到的最好的老师大概就是这位了,虽然他只给我讲过<java程序设计>和< ...

  6. textContent 与innerText

    转自下面这位大神: http://zhangyaochun.iteye.com/blog/1391370 其实关于这textContent与innerText有很多碎碎的东西,不过个人觉得还是一个不错 ...

  7. 在配置dubbo框架的时候出现dubbo:application标签无法识别问题。

    这原因是因为目前 http://code.alibabatech.com/schema/dubbo/dubbo.xsd标签库打不开.所以我们需要在dubbo的jar包里面引入一个dubbo.xsd 解 ...

  8. Flume 远程写HDFS

    现在的需求是在一台Flume采集机器上,往Hadoop集群上写HDFS,该机器没有安装Hadoop. 这里的Flume版本是1.6.0,Hadoop版本是2.7.1. 把Hadoop集群的hdfs-s ...

  9. weed-fs 基础测试

    =================== 启动 master 端口:9333 =================== sunsl@test-server:~$ weed master I0102 15: ...

  10. mongo 查找附近点

    db.runCommand({geoNear:"demo", near: { type: "Point" , coordinates: [118.134535, ...