转自:http://blog.csdn.net/java886o/article/details/9046967

  1. #import <Foundation/Foundation.h>
  2. #import "Person.h"
  3. int main(int argc, const char * argv[])
  4. {
  5. @autoreleasepool {
  6. //-----------第1种归档方式---------
  7. //1.使用NSKeyedArchiver 归档对象到文件(对象序列化,持久化)
  8. NSArray* arrays = [[NSArray alloc]initWithObjects:@"111",@"222", nil];
  9. NSString* userPath = NSHomeDirectoryForUser(@"3g2win");
  10. NSLog(@"userPath=%@",userPath);
  11. NSString* bakFilePath = [userPath stringByAppendingFormat:@"/test.txt"];
  12. NSLog(@"bakFilePath=%@",bakFilePath);
  13. NSLog(@"归档前的数组:\n\n%@",arrays);
  14. //归档
  15. if ([NSKeyedArchiver archiveRootObject:arrays toFile:bakFilePath]){
  16. NSLog(@"归档成功...\n\n");
  17. }else {
  18. NSLog(@"归档失败...\n\n");
  19. }
  20. //2.使用NSKeyedUnarchiver 解归档文件到对象(反序列化)
  21. //解归档
  22. NSArray* srcArr =  [NSKeyedUnarchiver unarchiveObjectWithFile:bakFilePath];
  23. NSLog(@"解归档后的数组:\n\n%@",srcArr);
  24. //------------第2种归档方式---------
  25. //归档
  26. NSMutableData* data = [NSMutableData alloc];
  27. NSKeyedArchiver* archiver = [NSKeyedArchiver alloc];
  28. [archiver initForWritingWithMutableData:data];
  29. [archiver encodeObject:@"张三" forKey:@"name"];
  30. [archiver encodeInt:25 forKey:@"age"];
  31. [archiver encodeFloat:5200.5F forKey:@"money"];
  32. [archiver finishEncoding];
  33. [data writeToFile:@"/Users/3g2win/11111111111.txt" atomically:YES];
  34. //解归档
  35. NSMutableData* data2 = [NSMutableData dataWithContentsOfFile:@"/Users/3g2win/11111111111.txt"];
  36. NSKeyedUnarchiver* unArchiver = [NSKeyedUnarchiver alloc];
  37. [unArchiver initForReadingWithData:data2];
  38. NSLog(@"name=%@",[unArchiver decodeObjectForKey:@"name"]);
  39. NSLog(@"age=%d",[unArchiver decodeIntForKey:@"age"]);
  40. NSLog(@"money=%f",[unArchiver decodeFloatForKey:@"money"]);
  41. //-------------自定义对象的归档和解归档------------
  42. Person* zhao6 = [[Person alloc] initWithName:@"赵六" withAge:18];
  43. [zhao6 display];
  44. //归档
  45. [NSKeyedArchiver archiveRootObject:zhao6 toFile:@"/Users/3g2win/zhao6.txt"];
  46. //解档
  47. Person* newZhao6 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/3g2win/zhao6.txt"];
  48. [newZhao6 display];
  49. }
  50. return 0;
  51. }
#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

  1. #import <Foundation/Foundation.h>
  2. //必须实现NSCoding协议才能够归档解归档自定义类
  3. @interface Person : NSObject <NSCoding>
  4. {
  5. NSString* name;
  6. int age;
  7. }
  8. @property (nonatomic,assign) NSString* name;
  9. @property (nonatomic,assign) int age;
  10. - (id) initWithName:(NSString*) _name withAge:(int) _age;
  11. - (void) display;
  12. @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

    1. #import "Person.h"
    2. #define NAME @"NAME"
    3. #define AGE @"AGE"
    4. @implementation Person
    5. @synthesize name;
    6. @synthesize age;
    7. - (id) initWithName:(NSString*) _name withAge:(int) _age {
    8. if (self = [super init]) {
    9. self.name = _name;
    10. self.age = _age;
    11. }
    12. return self;
    13. }
    14. - (void) display {
    15. NSLog(@"Person Name : %@\t Age :%d",name,age);
    16. }
    17. //归档编码
    18. - (void)encodeWithCoder:(NSCoder *)aCoder {
    19. [aCoder encodeObject:name forKey:NAME];
    20. [aCoder encodeInt:age forKey:AGE];
    21. }
    22. //解归档解码
    23. - (id)initWithCoder:(NSCoder *)aDecoder {
    24. if (self = [super init]) {
    25. self.name = [aDecoder decodeObjectForKey:NAME];
    26. self.age = [aDecoder decodeIntForKey:AGE];
    27. }
    28. return self;
    29. }
    30. @end

【IPHONE开发-OBJECTC入门学习】对象的归档和解归档的更多相关文章

  1. 【IPHONE开发-OBJECTC入门学习】复制对象,深浅复制

    转自:http://blog.csdn.net/java886o/article/details/9046273 #import <Foundation/Foundation.h> int ...

  2. 【IPHONE开发-OBJECTC入门学习】文件的操作,读写复制文件

    转自:http://blog.csdn.net/java886o/article/details/9041547 FileTools.h FileTools.m #import "FileT ...

  3. swift 之归档和解归档

    swift 之归档和解归档 数据持久化的方式有很多种,归档是其中的一种,说起数据持久化的方式,iOS 中基本有以下几种方式:sqlite存储.coredata存储.UserDefault存储.归档.p ...

  4. iOS:文件归档和解归档的详解和使用

    文件归档和解归档: 用途: 所谓文件归档,就是把需要存储的对象数据存储到沙盒的Documents目录下的文件中,即存储到了磁盘上,实现数据的持久性存储和备份.解归档,就是从磁盘上读取该文件下的数据,用 ...

  5. iOS 数据存储 - 归档和解归档

    这里的归档主要是用于自定义类的归档和解档.我们这里使用NSKeyedArchiver和NSKeyedUnarchiver来归档和解档. 注意:自己定义的类需要实现<NSCoding>,如: ...

  6. HoloLens开发手记 - 入门学习阶段总结

    伴随着数月的期待,终于拿到了预订的HoloLens开发者版本套件.随着VR/AR/MR技术的热潮,国内外均对它们的应用与盈利前景持有积极的预期,这也直接导致了国内外当前投资VR/AR/MR技术的热潮. ...

  7. 开发环境入门 linux基础 (部分) 归档 压缩 Vi编译器 系统分区

    归档 压缩 Vi编译器 系统分区 1.使用cat命令进行文件的纵向合并          1) 使用cat命令实现文件的纵向合并:          a) 例如:将用户信息数据库文件和组信息数据库文件 ...

  8. 树莓派开发板入门学习笔记1:[转]资料收集及树莓派系统在Ubuntu安装

    参考教程(微雪课堂):http://www.waveshare.net/study/portal.php 树莓派实验室: http://shumeipai.nxez.com/2014/12/21/us ...

  9. 迅为iTOP-4412物联网开发板入门学习高手进阶项目开发超树莓派

    免费视频教程: 为初学者精心录制的整套视频教程全部免费,随IT技术发展而不断增添的视频教程仍然免费!一支有经验的工程师团队会始终成为您的后盾. 项目实战---全开源: 手机远程控制开发板 门禁系统 W ...

随机推荐

  1. 【JavaWeb】Ajax基础

    Ajax介绍 Asynchronous JavaScript And XML(异步的JavaScript和XML): Ajax可以在不刷新页面的前提下,进行页面局部更新: Ajax不是新的技术,Aja ...

  2. CentOS7破解root密码

    第一步: reboot重启系统,进入修改密码步骤,出现此界面后,按e进行编辑 2.找到linux16这一段中的 ro  crashkernel=xxx, 将 ro 改成rw init=/sysroot ...

  3. 手写面试编程题- 数组去重 深拷贝 获取文本节点 设置奇数偶数背景色 JS中检测变量为string类型的方法 第6题闭包 将两个数组合并为一个数组 怎样添加、移除、移动、复制、创建和查找节点? 继承 对一个数组实现随机排序 让元素水平 垂直居中的三种方式 通过jQuery的extend方法实现深拷贝

    第1题==>实现数组去重 通过 new Set(数组名) // var arr = [12, 12, 3, 4, 5, 4, 5, 6, 6]; // var newarr1 = new Set ...

  4. phpstudy配置虚拟域名

    之前有一篇使用xampp配置虚拟域名,但是不同公司使用的集成环境不同,(xampp是我自己用的,别误解(><) !)这次使用的phpstudy,相比较而言,phpstudy更简单一点 首先 ...

  5. 4. Go语言—值类型和引用类型

    一.值类型 1. 定义 ​ 变量直接存储的值,内存通常在栈中分配: var i = 5 -> i-->5 2. 应用 int.float.bool.string.数组.struct 二.引 ...

  6. web-never give up

    打开题目连接 ?id=1 ,疑是注入点 但是输入其他数字无果 打开源码,发现注释有网页链接 打开连接123.206.87.240:8006/test/1p.html 发现回到了bugku的论坛首页,应 ...

  7. skkyk:题解 洛谷P2420 【让我们异或吧】lca+xor前缀和

    刚学了LCA,写篇题解巩固一下 首先题目有误: (A是否是男生 )xor( B是否是男生)=A和B是否能够成为情侣,这句话显然是错误的qwq 对于这道题,容易看出,对于待处理的两个点,只要我们找到他的 ...

  8. H5视频、音频不能自动播放,Uncaught (in promise) DOMException: play() failed because the user didn't

    错误原因:Chrome的autoplay政策在2018年4月做了更改. 解决办法: 第一步,在chrome浏览器中输入:chrome://flags/#autoplay-policy 第二步,在Aut ...

  9. [算法模版]Tarjan爷爷的几种图论算法

    [算法模版]Tarjan爷爷的几种图论算法 前言 Tarjan爷爷发明了很多图论算法,这些图论算法有很多相似之处(其中一个就是我都不会).这里会对这三种算法进行简单介绍. 定义 强连通(strongl ...

  10. Python连载26-shelve模块

    一.持久化 --shelve 持久化工具 (1)作用:类似字典,用kv对保存数据,存取方式类似于字典 (2)例子:通过一下案例创建了一个数据库,第二个程序我们读取了数据库 #使用shelve创建文件并 ...