【IPHONE开发-OBJECTC入门学习】对象的归档和解归档
转自:http://blog.csdn.net/java886o/article/details/9046967
- #import <Foundation/Foundation.h>
- #import "Person.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- //-----------第1种归档方式---------
- //1.使用NSKeyedArchiver 归档对象到文件(对象序列化,持久化)
- NSArray* arrays = [[NSArray alloc]initWithObjects:@"111",@"222", nil];
- NSString* userPath = NSHomeDirectoryForUser(@"3g2win");
- NSLog(@"userPath=%@",userPath);
- NSString* bakFilePath = [userPath stringByAppendingFormat:@"/test.txt"];
- NSLog(@"bakFilePath=%@",bakFilePath);
- NSLog(@"归档前的数组:\n\n%@",arrays);
- //归档
- if ([NSKeyedArchiver archiveRootObject:arrays toFile:bakFilePath]){
- NSLog(@"归档成功...\n\n");
- }else {
- NSLog(@"归档失败...\n\n");
- }
- //2.使用NSKeyedUnarchiver 解归档文件到对象(反序列化)
- //解归档
- NSArray* srcArr = [NSKeyedUnarchiver unarchiveObjectWithFile:bakFilePath];
- NSLog(@"解归档后的数组:\n\n%@",srcArr);
- //------------第2种归档方式---------
- //归档
- NSMutableData* data = [NSMutableData alloc];
- NSKeyedArchiver* archiver = [NSKeyedArchiver alloc];
- [archiver initForWritingWithMutableData:data];
- [archiver encodeObject:@"张三" forKey:@"name"];
- [archiver encodeInt:25 forKey:@"age"];
- [archiver encodeFloat:5200.5F forKey:@"money"];
- [archiver finishEncoding];
- [data writeToFile:@"/Users/3g2win/11111111111.txt" atomically:YES];
- //解归档
- NSMutableData* data2 = [NSMutableData dataWithContentsOfFile:@"/Users/3g2win/11111111111.txt"];
- NSKeyedUnarchiver* unArchiver = [NSKeyedUnarchiver alloc];
- [unArchiver initForReadingWithData:data2];
- NSLog(@"name=%@",[unArchiver decodeObjectForKey:@"name"]);
- NSLog(@"age=%d",[unArchiver decodeIntForKey:@"age"]);
- NSLog(@"money=%f",[unArchiver decodeFloatForKey:@"money"]);
- //-------------自定义对象的归档和解归档------------
- Person* zhao6 = [[Person alloc] initWithName:@"赵六" withAge:18];
- [zhao6 display];
- //归档
- [NSKeyedArchiver archiveRootObject:zhao6 toFile:@"/Users/3g2win/zhao6.txt"];
- //解档
- Person* newZhao6 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/3g2win/zhao6.txt"];
- [newZhao6 display];
- }
- return 0;
- }
#import <Foundation/Foundation.h>
#import "Person.h" int main(int argc, const char * argv[])
{ @autoreleasepool { //-----------第1种归档方式--------- //1.使用NSKeyedArchiver 归档对象到文件(对象序列化,持久化)
NSArray* arrays = [[NSArray alloc]initWithObjects:@"111",@"222", nil]; NSString* userPath = NSHomeDirectoryForUser(@"3g2win"); NSLog(@"userPath=%@",userPath); NSString* bakFilePath = [userPath stringByAppendingFormat:@"/test.txt"]; NSLog(@"bakFilePath=%@",bakFilePath); NSLog(@"归档前的数组:\n\n%@",arrays); //归档 if ([NSKeyedArchiver archiveRootObject:arrays toFile:bakFilePath]){
NSLog(@"归档成功...\n\n");
}else {
NSLog(@"归档失败...\n\n");
} //2.使用NSKeyedUnarchiver 解归档文件到对象(反序列化)
//解归档
NSArray* srcArr = [NSKeyedUnarchiver unarchiveObjectWithFile:bakFilePath]; NSLog(@"解归档后的数组:\n\n%@",srcArr); //------------第2种归档方式--------- //归档
NSMutableData* data = [NSMutableData alloc];
NSKeyedArchiver* archiver = [NSKeyedArchiver alloc];
[archiver initForWritingWithMutableData:data];
[archiver encodeObject:@"张三" forKey:@"name"];
[archiver encodeInt:25 forKey:@"age"];
[archiver encodeFloat:5200.5F forKey:@"money"];
[archiver finishEncoding];
[data writeToFile:@"/Users/3g2win/11111111111.txt" atomically:YES]; //解归档
NSMutableData* data2 = [NSMutableData dataWithContentsOfFile:@"/Users/3g2win/11111111111.txt"];
NSKeyedUnarchiver* unArchiver = [NSKeyedUnarchiver alloc];
[unArchiver initForReadingWithData:data2]; NSLog(@"name=%@",[unArchiver decodeObjectForKey:@"name"]); NSLog(@"age=%d",[unArchiver decodeIntForKey:@"age"]); NSLog(@"money=%f",[unArchiver decodeFloatForKey:@"money"]); //-------------自定义对象的归档和解归档------------
Person* zhao6 = [[Person alloc] initWithName:@"赵六" withAge:18]; [zhao6 display]; //归档
[NSKeyedArchiver archiveRootObject:zhao6 toFile:@"/Users/3g2win/zhao6.txt"]; //解档 Person* newZhao6 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/3g2win/zhao6.txt"]; [newZhao6 display]; }
return 0;
}
Person.h
- #import <Foundation/Foundation.h>
- //必须实现NSCoding协议才能够归档解归档自定义类
- @interface Person : NSObject <NSCoding>
- {
- NSString* name;
- int age;
- }
- @property (nonatomic,assign) NSString* name;
- @property (nonatomic,assign) int age;
- - (id) initWithName:(NSString*) _name withAge:(int) _age;
- - (void) display;
- @end
#import <Foundation/Foundation.h>
//必须实现NSCoding协议才能够归档解归档自定义类
@interface Person : NSObject <NSCoding>
{
NSString* name;
int age;
}
@property (nonatomic,assign) NSString* name;
@property (nonatomic,assign) int age;
- (id) initWithName:(NSString*) _name withAge:(int) _age;
- (void) display;
@end
Person.m
- #import "Person.h"
- #define NAME @"NAME"
- #define AGE @"AGE"
- @implementation Person
- @synthesize name;
- @synthesize age;
- - (id) initWithName:(NSString*) _name withAge:(int) _age {
- if (self = [super init]) {
- self.name = _name;
- self.age = _age;
- }
- return self;
- }
- - (void) display {
- NSLog(@"Person Name : %@\t Age :%d",name,age);
- }
- //归档编码
- - (void)encodeWithCoder:(NSCoder *)aCoder {
- [aCoder encodeObject:name forKey:NAME];
- [aCoder encodeInt:age forKey:AGE];
- }
- //解归档解码
- - (id)initWithCoder:(NSCoder *)aDecoder {
- if (self = [super init]) {
- self.name = [aDecoder decodeObjectForKey:NAME];
- self.age = [aDecoder decodeIntForKey:AGE];
- }
- return self;
- }
- @end
【IPHONE开发-OBJECTC入门学习】对象的归档和解归档的更多相关文章
- 【IPHONE开发-OBJECTC入门学习】复制对象,深浅复制
转自:http://blog.csdn.net/java886o/article/details/9046273 #import <Foundation/Foundation.h> int ...
- 【IPHONE开发-OBJECTC入门学习】文件的操作,读写复制文件
转自:http://blog.csdn.net/java886o/article/details/9041547 FileTools.h FileTools.m #import "FileT ...
- swift 之归档和解归档
swift 之归档和解归档 数据持久化的方式有很多种,归档是其中的一种,说起数据持久化的方式,iOS 中基本有以下几种方式:sqlite存储.coredata存储.UserDefault存储.归档.p ...
- iOS:文件归档和解归档的详解和使用
文件归档和解归档: 用途: 所谓文件归档,就是把需要存储的对象数据存储到沙盒的Documents目录下的文件中,即存储到了磁盘上,实现数据的持久性存储和备份.解归档,就是从磁盘上读取该文件下的数据,用 ...
- iOS 数据存储 - 归档和解归档
这里的归档主要是用于自定义类的归档和解档.我们这里使用NSKeyedArchiver和NSKeyedUnarchiver来归档和解档. 注意:自己定义的类需要实现<NSCoding>,如: ...
- HoloLens开发手记 - 入门学习阶段总结
伴随着数月的期待,终于拿到了预订的HoloLens开发者版本套件.随着VR/AR/MR技术的热潮,国内外均对它们的应用与盈利前景持有积极的预期,这也直接导致了国内外当前投资VR/AR/MR技术的热潮. ...
- 开发环境入门 linux基础 (部分) 归档 压缩 Vi编译器 系统分区
归档 压缩 Vi编译器 系统分区 1.使用cat命令进行文件的纵向合并 1) 使用cat命令实现文件的纵向合并: a) 例如:将用户信息数据库文件和组信息数据库文件 ...
- 树莓派开发板入门学习笔记1:[转]资料收集及树莓派系统在Ubuntu安装
参考教程(微雪课堂):http://www.waveshare.net/study/portal.php 树莓派实验室: http://shumeipai.nxez.com/2014/12/21/us ...
- 迅为iTOP-4412物联网开发板入门学习高手进阶项目开发超树莓派
免费视频教程: 为初学者精心录制的整套视频教程全部免费,随IT技术发展而不断增添的视频教程仍然免费!一支有经验的工程师团队会始终成为您的后盾. 项目实战---全开源: 手机远程控制开发板 门禁系统 W ...
随机推荐
- 5个 JS 解构有趣的用途
摘要: 玩转ES6解构赋值. 原文:5个 JS 解构有趣的用途 译者:前端小智 1. 交换变量 通常交换两个变量的方法需要一个额外的临时变量,来看看例子: let a = 1; let b = 2; ...
- SRDC - ORA-1628: Checklist of Evidence to Supply (Doc ID 1682729.1)
SRDC - ORA-1628: Checklist of Evidence to Supply (Doc ID 1682729.1) Action Plan 1. Execute srdc_db_u ...
- RAID 独立磁盘冗余阵列 - redundant array of independent disks
RAID: RAID全称是独立磁盘冗余阵列(Redundant Array of Independent Disks),基本思想是把多个磁盘组合起来,组合一个磁盘阵列组,使得性能大幅提高. RAID ...
- 5G浪潮来袭,程序员在风口中有何机遇
导读:本文共2894字,预计阅读时间为9分钟.通过阅读本文,你将了解到5G的优势.即将燃爆的领域以及程序员在快速发展的5G产业中所需关注的技术. 5G时代已经来临 随着中美5G主导权之战的持续发酵,5 ...
- 常见的Dos命令大全
打开cmd: Win键+R 输入cmd; 常用的Dos命令: 1.盘符切换: 2.打开文件目录: dir 3.清理屏幕: cls 4.退出: exit 5.查看本机IP地址:ipconfig ...
- c# WF 第6节 MDI窗体
本节内容: 1:SDI 窗体是什么 2: MDI 窗体是什么 3:如何创建MDI窗体 1:SDI 窗体是什么 SDI 窗体 : single-document interface 单一的窗体:上篇的启 ...
- 动态添加Redis密码认证的方法
1.定制jedis 对redis返回的错误的处理,做两处修改: 忽略 (error) ERR Client sent AUTH, but no password is set.使配置了密码的jedis ...
- C++ int 和string互相转化
1.int转换成string );//"-12" );//"12" +);//"1" 2.string转换成int );//"-1 ...
- day47_9_6(前端之js)
一.js发展. 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织ECMA,希望这门语言能够成为国际标准.次年,ECMA发布262号 ...
- 【CodeChef EDGEST】Edges in Spanning Trees(树链剖分+树上启发式合并)
点此看题面 大致题意: 给你两棵\(n\)个点的树,对于第一棵树中的每条边\(e_1\),求存在多少条第二棵树中的边\(e_2\),使得第一棵树删掉\(e_1\)加上\(e_2\).第二棵树删掉\(e ...