【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 ...
随机推荐
- Mysql8.0主从复制搭建,shardingsphere+springboot+mybatis读写分离
1.安装mysql8.0 首先需要在192.167.3.171上安装JDK. 下载mysql安装包,https://dev.mysql.com/downloads/,找到以下页面下载. 下载后放到li ...
- css为图片添加一层蒙版并在上层显示文字等
效果图: 代码如下: <div class="row" style="width:100%; position:relative;z-index:1;margin: ...
- [PHP] 基于redis的分布式锁防止高并发重复请求
需求:我们先举个某系统验证的列子:(A渠道系统,业务B系统,外部厂商C系统) (1)B业务系统调用A渠道系统,验证传入的手机.身份证.姓名三要素是否一致. (2)A渠道系统再调用外部厂商C系统. (3 ...
- cpu 乱序执行与问题【转】
转自:https://blog.csdn.net/lizhihaoweiwei/article/details/50562732 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议 ...
- IAR调试查看程序时间
在无仿真器情况下设置: 调试仿真,点击view菜单下registers 软件仿真时计算两断点CYCLECOUNTER(在CPU registers中)的差值,乘以指令周期(MCLK)便是执行时间
- (二)Amazon Lightsail 部署LAMP应用程序之部署单片LAMP应用程序
部署单片LAMP应用程序 简介:通过复制应用程序代码并提供链接PHP前端和本地MySQL数据库的参数,将LAMP对战应用程序部署到先前启动的Lightsail实例中.完成后,Apache/PHP前端和 ...
- 【BZOJ2437】[Noi2011]兔兔与蛋蛋(博弈+二分图)
传送门 题意: 给出一个\(n*m\)的棋盘,上面有若干个黑色棋子,若干个白色棋子,还有一个空格. 每次先手选择一个空格,将它与相邻的某个白色棋子交换:后手则选择一个空格,与相邻的某个黑色棋子交换. ...
- Bliss OS 12.1下载 PC上Android10体验
下载也不是一帆风顺啊 这是设计者的secret: https://forum.xda-developers.com/android/software/bliss-os-x86-pc-s-12-x-de ...
- Note | Ubuntu
目录 0. 教程 1. 安装 2. 系统 0. 教程 <Linux就该这么学>:https://www.cnblogs.com/RyanXing/p/9462850.html 1. 安装 ...
- AChartEngine折线图实例
最近做项目要用到图表,在网上找相关的解决方案找了很久,搜到最多的就是这个框架,所以就开始研究下怎么使用,首先研究的就是折线图,如是做了一个实例. AChartEngine下载地址:http://cod ...