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. 苹 ...
随机推荐
- mybatis的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 利用python合并两个文件
1格式如下 在做利用zabbix的api来批量添加主机的时候,需要处理ip和hostname,在借用别人写的py程序的基础上,自己有改装了以下脚本,为自己使用.需要时ip和hostname为一个统一格 ...
- 用类(function(){})()实现点击显示index索引值的详解
code: <script type="text/javascript"> ; i < ; i++){ var btn = document.createElem ...
- 怎么样修改小猪cms(从功能库添加)模块关键字
需求:修改或者添加从功能库添加中的关键字 这里以添加咨询投诉为列: 找到wwwroot\PigCms\Lib\Action\User目录下的LinkAction.class.php文件(手动找不到直接 ...
- COGS729. [网络流24题] 圆桌聚餐
«问题描述:假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri(i=1,2,3...m), .会议餐厅共有n张餐桌,每张餐桌可容纳c i(i=1,2...n) 个代表就餐.为了 ...
- <<< javascript地址栏,代码
不伤及服务器,可以用来测试项目效果 修改网页.不修改服务器端任何网页页面 javascript:document.body.contentEditable='true';document.design ...
- Unicode, UTF, ASCII, ANSI format differences
Going down your list: "Unicode" isn't an encoding, although unfortunately, a lot of docume ...
- Win7---------专区
待完善中-------------------------------------- 1.0Win7来历: 不算上最早的Windows版本从 95 98 2000 ME XP 2003 Vis ...
- LoadRunner 函数之 web_add_cookie
简单示例: Action() { // 添加cookie web_add_cookie("is_login=True;path=/;domain=10.1.102.75"); // ...
- 如何让ConfigurationManager打开任意的配置文件
VisualStudio的配置文件很好很强大,用来保存数据库连接字符串或键值对都非常方便,只需要通过ConfigurationManager的ConnectionStrings或AppSettings ...