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. 苹 ...
随机推荐
- HTML中      等6种空白空格的区别
HTML提供了5种空格实体(space entity),它们拥有不同的宽度,非断行空格( )是常规空格的宽度,可运行于所有主流浏览器.其他几种空格 ( )在不同浏览器中宽度各异. ...
- 深入理解Java:注解
注解作用:每当你创建描述符性质的类或者接口时,一旦其中包含重复性的工作,就可以考虑使用注解来简化与自动化该过程. Java提供了四种元注解,专门负责新注解的创建工作. 元注解 元注解的作用就是负责注解 ...
- 一个五年 Android 开发者百度、阿里、聚美、映客的面试心经
花絮 也许会有人感叹某些人的运气比较好,但是他们不曾知道对方吃过多少苦,受过多少委屈.某些时候就是需要我们用心去发现突破点,然后顺势而上,抓住机遇,那么你将会走向另外一条大道,成就另外一个全新的自我. ...
- RabbitMQ消息队列在PHP下的应用
消息队列的实现中,RabbitMQ以其健壮和可靠见长.公司的项目中选择了它作为消息队列的实现.关于MQ的机制和原理网上有很多文章可以看,这里就不再赘述,只讲几个比较容易混淆的问题 1,binding ...
- php设置浏览器响应时间
ini_set('max_execution_time', '0'); ‘0’表示不受时间限制,一般默认30s;
- 剑指offer 面试题64 数据流的中位数
struct cmp { bool operator()(double a, double b) { return a > b; } }; class Solution { public: vo ...
- VOC2007检测任务的评估标准
VOC2007数据集使用mAP值作为检测算法检测结果的性能评估得分.mAP意思是mean Average Precision,Precision是指精度,Average Precision是指11个等 ...
- RabbitMQ headers Exchange
Headers Exchange headers也是一种交换机类型,但是在rabbitmq官网中的教程中并没有说到.资料也很少,但是找一找总会有的. headers与direct的模式不同,不是使用r ...
- hackerrank Similar Pair
传送门 Problem Statement You are given a tree where each node is labeled from 1 to n. How many similar ...
- Objective-C总Runtime的那点事儿(一)消息机制
最近在找工作,Objective-C中的Runtime是经常被问到的一个问题,几乎是面试大公司必问的一个问题.当然还有一些其他问题也几乎必问,例 如:RunLoop,Block,内存管理等.其他的问题 ...