#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. 《Pro Git》笔记2:Git基础操作

    第二章 Git基础 Git基础包括:版本库的创建和获取,文件添加修改提交等基本操作,状态查询,远程版本库管理和同步,打标签. 1.取得项目的Git版本库 基于Git的工作流要以Git版本库为基础,即可 ...

  2. ThinkPHP 3.2.2 视图模板中使用字符串截取函数

    在项目的 Common/function.php 文件里( 项目结构如图 ) 添加函数: /*字符串截断函数+省略号*/ function subtext($text, $length) { if(m ...

  3. charles 常用设置

    一.过滤网络请求 通常情况下,我们需要对网络请求进行过滤,只监控向指定目录服务器上发送的请求.对于这种需求,我们有2种办法. 1.在主界面的中部的Filter栏中填入需要过滤出来的关键字.例如我们的服 ...

  4. 自定义view实现侧滑菜单

    自定义View public class SlidingMenu extends HorizontalScrollView { private int mScreenWidth; private in ...

  5. ios-滚动导航条页面

    // ViewController.m #import "ViewController.h" #import "ScrollSliderView.h" @int ...

  6. C#winform中ListView的使用

    使用ListView模仿Windows系统的资源管理器界面,实现文件(夹)的浏览.重命名.删除及查询等功能,主要功能界面展示如下: 1.MainForm.cs及MainForm.Designer.cs ...

  7. C#winfrom中splitContainer的用法

    常用属性 (1).splitContainer不能被鼠标直接点击选中,可通过右击->选择splitContainer选中. (2).属性IsSplitterFixed用于设置拆分器能否移动. ( ...

  8. PHP5.4安装xhprof扩展[不要去pecl下载]

    HP5.3或之前版本可以去pecl(http://pecl.php.net)下载xhprof扩展安装. 但pecl上的版本不支持PHP5.4 可以到github上的xhprof库中下载:https:/ ...

  9. Memcached原理深度分析详解

    Memcached是 danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能.关于这个东 西,相信很多人都用过,本文意在通 ...

  10. <?php set_time_limit(10);

    <?php include('w_fun.php'); set_time_limit(10); Fatal error: Maximum execution time of 10 seconds ...