iOS 归档
#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 归档的更多相关文章
- iOS 归档archive使用方法
归档是一种很常用的文件储存方法,几乎任何类型的对象都能够被归档储存,文件将被保存成自定 义类型的文件,相对于NSUserDefault具有更好的保密性. 1.使用archiveRootObject ...
- [转载]iOS 归档操作 NSCoding
最近一个项目需要保存到本地文件,想用plist,但是发现很多内容是自定义的,于是只能自己归档接档.不难,找了一篇范文大家保存一下,方便以后学习使用. 转自:http://mobile.51cto.co ...
- iOS——归档对象的创建,数据写入与读取
归档(archiving)是指另一种形式的序列化,但它是任何对象都可以实现的更常规的模型.专门编写用于保存数据的任何模型对象都应该支持归档.比属性列表多了很良好的伸缩性,因为无论添加多少对象,将这些对 ...
- IOS数据存储之归档/解档
前言: 前天学习了NSUserDefaults,我们知道NSUserDefaults不能保存自定义对象,所以我们今天来认识一下归档(NSKeyedArchiver)和解档(NSKeyedUnarchi ...
- Xcode5 + phoneGap2.9搭建ios开发环境-配置-测试-归档上传/phoneG...
前言: 小弟是做JAVA/Android的第一次搞这个ios,公司有mobile项目是使用phoneGap开发的,需要开发ios版本.什么都不会只能一点一点琢磨了……大神越过…… 原文链接:http: ...
- iOS阶段学习第18天笔记(Plist-Archiver-归档与解归档操作)
iOS学习(OC语言)知识点整理 一.归档与解归档的操作 1)归档是一个过程,将一个或多个对象存储起来,以便以后可以还原,包括将对象存入文件,以后再读取 将数据对象归档成plist文件 2)plist ...
- iOS开发——UI进阶篇(十一)应用沙盒,归档,解档,偏好设置,plist存储,NSData,自定义对象归档解档
1.iOS应用数据存储的常用方式XML属性列表(plist)归档Preference(偏好设置)NSKeyedArchiver归档(NSCoding)SQLite3 Core Data 2.应用沙盒每 ...
- iOS开发UI篇—ios应用数据存储方式(归档)
iOS开发UI篇—ios应用数据存储方式(归档) 一.简单说明 在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦: 偏好设置(将所有的东西都保存在同 ...
- iOS学习之应用数据存储1-属性列表、偏好设置、NSKeyedArchiver归档
iOS应用数据存储的常用方式(持久化方式) 属性列表(plist)归档(XML文件) Preference(偏好设置) NSKeyedArchiver归档(NSCoding) SQLite3 Core ...
随机推荐
- CSS兼容性(IE和Firefox)技巧大全
CSS对浏览器的兼容性有时让人很头疼,或许当你了解当中的技巧跟原理,就会觉得也不是难事,从网上收集了IE7,6与Fireofx的兼容性处理技巧并整理了一下.对于web2.0的过度,请尽量用xhtml格 ...
- AMD PerfStudio
用PerfStudio 抓DX11 OIT
- AngularJS 初识笔记
test.html: <!DOCTYPE html> <html lang="en" ng-app> <head> <meta chars ...
- Virtualbox虚拟Ubuntu共享文件夹设置
1. 启动ubuntu, 然后先virtualbox的安装增强功能,菜单:“设备(D)”-> "安装增强功能":然后进入ubuntu 系统,安装Vboxadditions_. ...
- Bootstrap页面布局5 - 响应式布局(格式)
旨在优化不同上网设备中页面显示的优化 响应式布局:就是根据浏览窗口的尺寸,改变页面的变化 原理:利用css的media-queries判断浏览窗口的尺寸,在CSS样式表中设置一些规则! 例如: 在&l ...
- cookie 操作
//创建并赋值 重新赋值也是这样操作 document.cookie="userId=828"; document.cookie="userName=hulk" ...
- JS初学者必备的几个经典案例(一)!!!
一:选中复选框按钮可用 和 倒计时10秒后按钮可用 这是倒计时10秒后按钮可用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- android Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine.
在当前工程目录中 gradle.properties 添加org.gradle.jvmargs=-XX\:MaxHeapSize\=256m -Xmx256m http://stackoverflow ...
- 深入GetMessage和PeekMessage
http://blog.csdn.net/fireseed/article/details/2176 http://www.cnblogs.com/sadier/articles/100948.htm ...
- C#委托初探
委托是一种定义方法签名的类型,可以与具有兼容签名的任何方法关联.您可以通过委托调用其中已添加的方法列表.委托用于将方法作为参数传递给其他方法.事件处理程序就是通过委托调用的方法.您可以创建一个自定义方 ...