iOS 归档archive使用方法
//获取根目录
NSString *homeDictionary = NSHomeDirectory();
//添加储存的文件名
NSString *homePath = [homeDictionary
stringByAppendingPathComponent:@"atany.archiver"];
//归档一个字符串
BOOL flag = [NSKeyedArchiver archiveRootObject:@"归档" toFile:homePath];
NSString* str=[NSKeyedUnarchiver unarchiveObjectWithFile:homePath];
//准备数据
CGPoint point = CGPointMake(1.0, 2.0);
NSString *info = @"坐标原点";
NSInteger value = ;
//获取Home目录并创建multi.archiver文件
NSString *multiHomePath = [NSHomeDirectory()
stringByAppendingPathComponent:@"multi.archiver"];
//创建NSMutableData对象 用来存储归档数据
NSMutableData *data = [[NSMutableData alloc]init];
NSKeyedArchiver *archvier = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:data];
//对多个对象进行归档
[archvier encodeCGPoint:point forKey:@"kPoint"];
[archvier encodeObject:info forKey:@"kInfo"];
[archvier encodeInteger:value forKey:@"kValue"];
[archvier finishEncoding];
//写入文件
[data writeToFile:multiHomePath atomically:YES];
NSMutableData *dataR = [[NSMutableData alloc]initWithContentsOfFile:multiHomePath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:dateR];
CGPoint pointR = [unarchiver decodeCGPointForKey:@"kPoint"];
NSString *infoR = [unarchiver decodeObjectForKey:@"kInfo"];
NSInteger valueR = [unarchiver decodeIntegerForKey:@"kValue"];
[unarchiver finishDecoding];
NSLog(@"%f,%f,%@,%d",pointR.x,pointR.y,infoR,valueR);
#import <Foundation/Foundation.h> @interface Archive : NSObject
@property (copy,nonatomic) NSString *name;
@property NSInteger age;
@property (copy,nonatomic) NSString *address;
@property (copy,nonatomic) UIImage *photo; @end
#import "Archive.h"
#define kNameKey @"NameKey"
#define kAgeKey @"AgeKey"
#define kAddress @"AddressKey"
#define kPhotoKey @"PhotoKey" @implementation Archive
@synthesize name = _name;
@synthesize age = _age;
@synthesize address = _address;
@synthesize photo = _photo; #pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_name forKey:kNameKey];
[aCoder encodeInteger:_age forKey:kAgeKey];
[aCoder encodeObject:_address forKey:kAddress];
[aCoder encodeObject:_photo forKey:kPhotoKey];
} - (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
_name = [aDecoder decodeObjectForKey:kNameKey];
_age = [aDecoder decodeIntegerForKey:kAgeKey];
_address = [aDecoder decodeObjectForKey:kAddress];
_photo = [aDecoder decodeObjectForKey:kPhotoKey];
}
return self;
} #pragma mark - NSCoping
- (id)copyWithZone:(NSZone *)zone {
Archive *copy = [[[self class] allocWithZone:zone] init];
copy.name = [self.name copyWithZone:zone];
copy.age = self.age;
copy.address = [self.address copyWithZone:zone];
copy.photo = self.photo;
return copy;
}
@end
//保存图片与归档
- (IBAction)save:(id)sender {
//准备数据
NSString *name = @"Exile";
NSInteger age = ;
NSString *address = @"Address";
UIImage *photo = [UIImage imageNamed:@"login.jpg"];
//存储数据到类
Archive *archivingData = [[Archive alloc] init];
archivingData.name = name;
archivingData.age = age;
archivingData.address = address;
archivingData.photo = photo;
//归档
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:data];
// archivingDate的encodeWithCoder 方法被调用
[archiver encodeObject:archivingData forKey:kArchivingDataKey];
[archiver finishEncoding];
//写入文件
[data writeToFile:self.archivingFilePath atomically:YES];
}
- (IBAction)loadArchive:(id)sender {
NSData *data = [[NSMutableData alloc]
initWithContentsOfFile:self.archivingFilePath];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:data];
//获得类
//initWithCoder方法被调用
Archive *archivingData = [unarchiver decodeObjectForKey:kArchivingDataKey];
[unarchiver finishDecoding];
//读取的数据
NSString *name = archivingData.name;
NSInteger age = archivingData.age;
NSString *address = archivingData.address;
self.imageView.image = archivingData.photo;
NSLog(@"%@||%d||%@",name,age,address);
}
iOS 归档archive使用方法的更多相关文章
- (转) ORACLE 正确删除归档日志的方法
ORACLE 正确删除归档日志的方法 我们都知道在controlfile中记录着每一个archivelog文件的相关信息,当然们在OS下把这些物理文件delete掉后,在我们的controlfile中 ...
- iOS-提高iOS开发效率的方法和工具
提高iOS开发效率的方法和工具 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先要说的肯定是IDE了,说到IDE,Xcode不能跑,当然你也可能同时 ...
- iOS中的过期方法和新的替代方法
关于iOS中的过期方法和新的替代方法 1.获取某些类的UINavigationBar的统一外观并设置UINavigationbar的背景 注:方法名改了但是基本使用方法不变 + (instancety ...
- opencv直线检测在c#、Android和ios下的实现方法
opencv直线检测在c#.Android和ios下的实现方法 本文为作者原创,未经允许,不得转载 :原文由作者发表在博客园:http://www.cnblogs.com/panxiaochun/p/ ...
- iOS开发——实用篇&提高iOS开发效率的方法和工具
提高iOS开发效率的方法和工具 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先要说的肯定是IDE了,说到IDE,Xcode不能跑,当然你也可能同时 ...
- iOS与HTML5交互方法总结(转)
今天小编在找技术文章的时候,发现这样一个标题:iOS与HTML5交互方法总结,怎么看着这么熟悉呢? 还以为是刚哥用了别的文章,点进去一看,原来是刚哥自己写的文章,他们转载的,而且还上了Dev St ...
- iOS UISearchController 的使用方法
iOS UISearchController 的使用方法 UISearchController 让用户在 UISearchBar 上输入搜索关键词,展示搜索结果或者进行其他操作.UISearchCon ...
- IOS常见的加密方法,常用的MD5和Base64
iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...
- iOS与HTML5交互方法总结(修正)
摘要 看了不少别人写的博客或者论坛,关于iOS与HTML5交互方法大概主要有5种方式: 1. 利用WKWebView进行交互(系统API) 2. 利用UIWebView进行交互(系统API) 3. 苹 ...
随机推荐
- RTC时钟
1.设置时间之前取消备份区域(BKP)写保护 主要有两部分组成 1.用来和APB1总线对接,对其进行读写操作 2. 预分频模块:在RTC_CR设置相应的允许,位每个TR_CLK周期中RTC产生一个中断 ...
- yii获取当前url和域名
<?php //当前域名 echo Yii::app()->request->hostInfo; //除域名外的URL echo Yii::app()->request-> ...
- ansible的SSH连接问题
问题描述: 在ansible安装完毕后一般需要以SSH的方式连接到需要进行管理的目标主机,一开始遇到了如下问题: # ansible -m ping all 10.200.xx.xx | UNREAC ...
- 黄聪:phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...
- noip2016十连测round3
A:平均数 题意:有一天,小 A 得到了一个长度为 n 的序列. 他把这个序列的所有连续子序列都列了出来,并对每一个子序列都求了其平均值,然后他把这些平均值写在纸上,并对它们进行排序,最后他报出了第 ...
- eclipse发布web项目到生产环境的方式汇总(tomcat)
参考: http://www.cnblogs.com/xiohao/p/3689832.html http://www.111cn.net/jsp/J2EE-EJB/90337.htm 我收集了几点: ...
- 动态规划(DP)基础
DP基础 简单dp 背包问题 记忆化搜索 简单dp 数字三角形 给一个数字构成的三角形,求从顶端走到底部的一条路径,使得路径上的和最大(或者最小). 1 2 3 6 5 4 Example_1 7 3 ...
- 【Alpha版本】十天冲刺——日志集合贴
No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 Day1 Day2 Day3 Day ...
- 自学 Java 怎么入门
自学 Java 怎么入门? 595赞同反对,不会显示你的姓名 给你推荐一个写得非常用心的Java基础教程:java-basic | 天码营 这个教程将Java的入门基础知识贯穿在一个实例中,逐 ...
- (转)Javascript本地存储小结
转自:https://i.cnblogs.com/EditPosts.aspx?opt=1 以下是原文: 1. 各种存储方案的简单对比 Cookies:浏览器均支持,容量为4KB UserData:仅 ...