#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. hrbust oj 1526+2028 树状数组

    冒泡排序中 如果一个数的后面的某个数和这个数不符合排序规则 那么这个数就会在未来的某次冒泡中与那个数进行交换 这里用到了 树状数组求逆序数的办法来做 需要注意的是2028并不可以改完数组大小后直接套1 ...

  2. Markdown基本用法

    Markdown基本用法 不同的编辑器对本文中的功能可能显示情况不一样,马克飞象可以完美支持和显示. 一.标题 h1标题 #h1标题 h1标题 h1标题 == h2标题 ##h2标题 h2标题 h2标 ...

  3. AngularJS 初识笔记

    test.html: <!DOCTYPE html> <html lang="en" ng-app> <head> <meta chars ...

  4. PHP正则表达式及实例

    PHP正则表达式及实例 博客分类: Php / Pear / Mysql / Node.js 正则表达式PHPWordPressFPApache  关联: 正则表达式 去除连续空白 + 获取url + ...

  5. Xamarin Visual Studio无法debug

    在Visual Studio中,Target IOS Device下拉框是禁用状态,无法选择. Xamarin论坛中有不少关于这个问题的,如下面这个帖子: http://forums.xamarin. ...

  6. maven run as(debug as)没有运行的选项时

    run as - run configration -maven build- goal目录下填上:tomcat:run即可

  7. faker image

    $faker->image http://placehold.it http://placekitten.com/g/200/300 带文字 https://placeholdit.imgix. ...

  8. Delphi xe7 FireMonkey / Mobile (Android, iOS)生成 QR Code完整实例

    这个实例在windows.OS X.IOS和Android等平台运行正常.本文参考这个网站提供的方法:http://zarko-gajic.iz.hr/firemonkey-mobile-androi ...

  9. (leetcode)Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  10. {$ecs_css_path}

    includes里的init.php的187-194行 if (!empty($_CFG['stylename'])) { $smarty->assign('ecs_css_path', 'th ...