#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[RootViewController alloc] init]; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end
#import "RootViewController.h"
#import "User.h"
@interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad]; User *user = [User userInfo];
user.name = @"LF";
user.address = @"广州";
NSLog(@"归档前===%@===%@",user.name,user.address);
//归档
[NSKeyedArchiver archiveRootObject:user toFile:[self fileToPath]];
//解归档
User *user2 = [NSKeyedUnarchiver unarchiveObjectWithFile:[self fileToPath]]; NSLog(@"归档后===%@===%@",user2.name,user2.address);
user2.address = @"北京";
[NSKeyedArchiver archiveRootObject:user2 toFile:[self fileToPath]];
//解归档
User *user3 = [NSKeyedUnarchiver unarchiveObjectWithFile:[self fileToPath]]; NSLog(@"重新归档后===%@===%@",user3.name,user3.address);
}
/**
* 获取文件路径
*/
- (NSString *)fileToPath{
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [docPath stringByAppendingString:@"userInfomation"];
NSLog(@"%@",path);
return path;
} @end
#import <Foundation/Foundation.h>

@interface User : NSObject<NSCoding>

@property(nonatomic, copy) NSString *name;
@property(nonatomic, copy) NSString *address; + (User*)userInfo; @end
#import "User.h"

#define userName @"name"
#define userAddress @"address" @implementation User #pragma mark 单例模式
static User *instance = nil;
+ (User*)userInfo{
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
instance = [[self alloc] init];
});
return instance;
} //归档
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:userName];
[aCoder encodeObject:_address forKey:userAddress];
} // 解归档
#pragma mark NSCoding协议方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:userName];
self.address = [aDecoder decodeObjectForKey:userAddress];
}
return self;
} @end

iOS 归档的更多相关文章

  1. iOS 归档archive使用方法

    归档是一种很常用的文件储存方法,几乎任何类型的对象都能够被归档储存,文件将被保存成自定 义类型的文件,相对于NSUserDefault具有更好的保密性.   1.使用archiveRootObject ...

  2. [转载]iOS 归档操作 NSCoding

    最近一个项目需要保存到本地文件,想用plist,但是发现很多内容是自定义的,于是只能自己归档接档.不难,找了一篇范文大家保存一下,方便以后学习使用. 转自:http://mobile.51cto.co ...

  3. iOS——归档对象的创建,数据写入与读取

    归档(archiving)是指另一种形式的序列化,但它是任何对象都可以实现的更常规的模型.专门编写用于保存数据的任何模型对象都应该支持归档.比属性列表多了很良好的伸缩性,因为无论添加多少对象,将这些对 ...

  4. IOS数据存储之归档/解档

    前言: 前天学习了NSUserDefaults,我们知道NSUserDefaults不能保存自定义对象,所以我们今天来认识一下归档(NSKeyedArchiver)和解档(NSKeyedUnarchi ...

  5. Xcode5 + phoneGap2.9搭建ios开发环境-配置-测试-归档上传/phoneG...

    前言: 小弟是做JAVA/Android的第一次搞这个ios,公司有mobile项目是使用phoneGap开发的,需要开发ios版本.什么都不会只能一点一点琢磨了……大神越过…… 原文链接:http: ...

  6. iOS阶段学习第18天笔记(Plist-Archiver-归档与解归档操作)

    iOS学习(OC语言)知识点整理 一.归档与解归档的操作 1)归档是一个过程,将一个或多个对象存储起来,以便以后可以还原,包括将对象存入文件,以后再读取 将数据对象归档成plist文件 2)plist ...

  7. iOS开发——UI进阶篇(十一)应用沙盒,归档,解档,偏好设置,plist存储,NSData,自定义对象归档解档

    1.iOS应用数据存储的常用方式XML属性列表(plist)归档Preference(偏好设置)NSKeyedArchiver归档(NSCoding)SQLite3 Core Data 2.应用沙盒每 ...

  8. iOS开发UI篇—ios应用数据存储方式(归档)

    iOS开发UI篇—ios应用数据存储方式(归档)  一.简单说明 在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦: 偏好设置(将所有的东西都保存在同 ...

  9. iOS学习之应用数据存储1-属性列表、偏好设置、NSKeyedArchiver归档

    iOS应用数据存储的常用方式(持久化方式) 属性列表(plist)归档(XML文件) Preference(偏好设置) NSKeyedArchiver归档(NSCoding) SQLite3 Core ...

随机推荐

  1. Guilty Gear Xrd 资源Rip(1)

    资源破解   首先先要下载GGXrd的PS3游戏,用psarc.exe先把游戏解包 http://files.cnblogs.com/TracePlus/psarc.exe.zip   下载UMode ...

  2. Ubuntu kill命令用法详解

    转自:Ubuntu kill命令用法详解 1. kill   作用:根据进程号杀死进程   用法: kill [信号代码] 进程ID   root@fcola:/# ps -ef | grep sen ...

  3. 嵌入式Linux的FTP服务端软件(stupid-ftpd)

    我自己试没成功 http://blog.csdn.net/gzshun/article/details/7358651

  4. memcached学习笔记3--telnet操作memcached

    方式: 一.telnet访问memcached缓存系统(主要用于教学,不讨论) telnet 127.0.0.1 11211     => telnet IP地址 端口号 //往Memcache ...

  5. 3.PHP内核探索:一次请求生命周期

    我们从未手动开启过PHP的相关进程,它是随着Apache的启动而运行的.PHP通过mod_php5.so模块和Apache相连(具体说来是SAPI,即服务器应用程序编程接口). PHP总共有三个模块: ...

  6. Scrapy入门教程

    关键字:scrapy 入门教程 爬虫 Spider作者:http://www.cnblogs.com/txw1958/出处:http://www.cnblogs.com/txw1958/archive ...

  7. coursera-miniproject stopwatch任务总结

    ---恢复内容开始--- 首先是miniproject的说明: combin text drawing in canvas with time to build digital stopwatch. ...

  8. 线性表集合A=A B

    大话数据结构 void union(List *a, List Lb) { int La_len, Lb_len, i; ElemType e; La_len = ListLength(La); Lb ...

  9. wajueji

    #include<stdio.h>int map[3]={42,3,99};int step[3]={0};int max=99999;void qian(){ int i=0; int ...

  10. svn update 失败 且 clean up失败解决方法

    当出现这种情况时,大多数是因为svn的数据库还有队列没处理完.而我们要做到仅仅是把队列清除即刻. 具体做法是: 1.下载sqlite3.exe下载地址:sqlite官网http://www.sqlit ...