ios之归档demo
ios对自定义对象的归档。首先需要实现NSCoding与NSCopying接口
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCoding, NSCopying>
@property (copy,nonatomic)NSString *name;
@property(assign,nonatomic)NSInteger age;
@end
需要重写接口的3个方法
#import "Person.h"
@implementation Person
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInteger:_age forKey:@"age"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
_name = [aDecoder decodeObjectForKey:@"name"];
_age =[aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
//#pragma mark NSCoping
- (id)copyWithZone:(NSZone *)zone {
Person *copy = [[[self class] allocWithZone:zone] init];
copy.name = [self.name copyWithZone:zone];
copy.age = self.age;
return copy;
}
//////////////////////////////////////////
归档的两种方法:
(1)使用NSUserDefaults
// 保存
Person *person = [[Person alloc] init];
person.name = _text.text;
person.age = (NSInteger)[_age text]; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:person];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"person"]; // 读取
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"person"];
NSArray *books = [NSKeyedUnarchiver unarchiveObjectWithData:data];
(2)使用写文件
// 保存文件目录
-(NSString*) appStorgeLocation{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:];//Documents目录
return [documentsDirectory stringByAppendingPathComponent:@"lee0000"];
} // 归档
Person *person = [[Person alloc] init];
person.name = _text.text;
person.age = (NSInteger)[_age text]; NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:person forKey:@"kArchivingDataKey"]; //archivingDate的encodeWithCoder方法被调用
[archiver finishEncoding];
//写入文件
[data writeToFile:[self appStorgeLocation] atomically:YES]; // 读取
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self appStorgeLocation]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; //获得类
Person *archivingData = [unarchiver decodeObjectForKey:@"kArchivingDataKey"];// initWithCoder方法被调用
[unarchiver finishDecoding];
ios之归档demo的更多相关文章
- 最全的iOS物理引擎demo
概述 最全的iOS物理引擎demo,实现重力.碰撞.推力.摆动.碰撞+重力.重力弹跳.仿摩拜单车贴纸效果.防iMessage滚动效果.防百度外卖首页重力感应等效果! 详细 代码下载:http://ww ...
- iOS苹果官方Demo合集
Mirror of Apple’s iOS samples This repository mirrors Apple’s iOS samples. Name Topic Framework Desc ...
- iOS中归档对象的创建,数据写入与读取
归档(archiving)是指另一种形式的序列化,但它是任何对象都可以实现的更常规的模型.专门编写用于保存数据的任何模型对象都应该支持归档.比属性列表多了很良好的伸缩性,因为无论添加多少对象,将这些对 ...
- iOS支付 IPAPayment demo iTunes Conection里面添加测试帐号,添加商品,实现购买过程
https://github.com/ccguo/IAPPaymentDemo 发一个demo
- 【转】NO.2、Appium之IOS第一个demo
接第一篇:Appium之iOS环境搭建 http://blog.csdn.net/clean_water/article/details/52946191 这个实例继承了unittest,重写了它的s ...
- iOS 数据归档----温故而知新
#import "StudyViewController.h" #import "person.h" @interface StudyViewControlle ...
- iOS微信支付demo运行报错解决如下
要接入微信支付的小伙伴,首先要下载一份官方demo(APP微信支付官方Demo下载),然后打开工程,准备大干一场. 1.编译报错 编译的时候居然直接报错了(orz) 错误提示: APP微信支付官方De ...
- iOS archiveRootObject 归档失败问题
归档失败问题出在路径上,NSHomeDirectory() NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocument ...
- ios socket(基础demo)
http://blog.sina.com.cn/s/blog_7a2f0a830101ecv4.html clinetSocket 1.viewcontroller.h @interface View ...
随机推荐
- Google浏览器被360劫持
最新更新!!! 新写的一个 Chrome浏览器被hao123劫持: http://www.cnblogs.com/tanrong/p/7815494.html 这两个博客结合着看吧,它们几乎包括 ...
- php 会话控制(Session会话控制)
php的session会话是通过唯一的会话ID来驱动的,会话ID是一个加密的随机数字,由php生成,在会话的生命周期中都会保存在客户端.客户端保存数据的地方只有cookie,所以php的会话ID一般保 ...
- 介绍activity文档翻译
原文链接:https://developer.android.google.cn/guide/components/activities/intro-activitiesSS 一, 对activit的 ...
- CPU的主频
CPU的主频,即CPU内核工作的时钟频率(CPU Clock Speed).通常所说的某某CPU是多少兆赫的(MHZ),而这个多少兆赫就是“CPU的主频”.很多人认为CPU的主频就是其运行速度,其实不 ...
- C#开发Unity游戏教程之Scene视图与脚本的使用
C#开发Unity游戏教程之Scene视图与脚本的使用 Unity中Scene视图的快捷操作 Scene视图是开发者开发游戏时,操作最频繁的视图.因为一旦一个游戏对象被添加到游戏的场景中,就需要首先使 ...
- Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题
C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...
- TCP状态知识总结(图解)
tcp状态: LISTEN:侦听来自远方的TCP端口的连接请求 SYN-SENT:再发送连接请求后等待匹配的连接请求 SYN-RECEIVED:再收到和发送一个连接请求后等待对方对连接请求的确认 ...
- Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)
最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...
- TVS二极管和稳压二极管的区别
TVS二极管和稳压二极管的区别 TVS管超过它的耐压值后,会瞬间导通短路,反应速度在ns级, 而稳压管是稳压作用的,超过它的稳压值,只要功率不超过它的耐受值,就会稳定在它的稳压值范围内. TVS是瞬态 ...
- sTM32 使用TIMx_CH1作为 Tx1F_ED 计数器时钟
环境:iar arm 5.3 stm32f103vbt6 使用PA.8 外部输入10Mhz的方波.可从systick中断得到数据4. 4×5000(预分频值)×1000(tick中断时间)=20MHz ...