【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 ...
随机推荐
- 模仿UIApplication创建单例
UIApplicationMain: 1.创建UIApplication--应用程序唯一标识:可设置状态栏.识别联网状态.设置数字.打电话.发邮件.发短信.打开网页 2.创建UIApplication ...
- [20190531]ORA-600 kokasgi1故障模拟与恢复.txt
[20190531]ORA-600 kokasgi1故障模拟与恢复.txt --//昨天看链接:http://www.xifenfei.com/2019/05/ora-600-kokasgi1-rec ...
- VMware Tools安装方法
安装VMware Tools的步骤 点击[虚拟机]选项中的[安装VMware Tools],此时在Ubuntu的桌面上就会出现一个光盘图标. 如果之前已经安装过了,[虚拟机]选项中应为[重新安装VMw ...
- (二)Amazon Lightsail 部署LAMP应用程序之部署单片LAMP应用程序
部署单片LAMP应用程序 简介:通过复制应用程序代码并提供链接PHP前端和本地MySQL数据库的参数,将LAMP对战应用程序部署到先前启动的Lightsail实例中.完成后,Apache/PHP前端和 ...
- 04-align-content 它对于当单行是没有效果的
/* 运用在父级元素上 align-content: 它通产与子元素的div{margin:10px 一起联合使用 }*/ ps==>用在子项出现换行的情况下,并是多行的情况下哦.运用在子 ...
- MNIST手写数字识别进阶:多层神经网络及应用(1)
# 一.载入数据 import tensorflow as tf import numpy as np #导入tensorflow提供的读取MNIST的模块 import tensorflow.exa ...
- 巡风扫描器web界面工作流程
这两周学习了巡风扫描器的搭建,也在学长的带领下看了各部分的下源代码,为了加深记忆,梳理一下巡风大体的工作流程,主要通过web端的页面分析,错误的地方还请大佬们多多指正. 整体看一下巡风的扫描流程:登陆 ...
- RabbitMQ的消息传输保障三个层级
这里只简单介绍一下三个层级,笔记摘录自<RabbitMQ实战指南>朱忠华作者 消息可靠传输一般是业务系统接入消息中间件时候首要考虑的问题,一般消息中间件的消息传输保障分为三个层级 1 A ...
- 判断101-200之间有多少个素数,并输出所有素数,方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。
<?php$sum=0;for($i=101;$i<=200;$i++){ for($j=2;$j<=sqrt($i);$j++) { if($i%$j==0 ...
- 题解:openjudge 1.11——01
题目 思路:二分查找 来,上代码 #include<cstdio> #include<iostream> using namespace std; +]; int n,m; i ...