//
// ViewController.m
// IOS_0113_本地存储
//
// Created by ma c on 16/1/13.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
#import "BWLoginDataModel.h"
#import "BWSecondViewController.h" @interface ViewController () @end @implementation ViewController
/*
数据本地化(数据持久化)- 本地存储
1.NSUserDefault属性列表存储 - 轻量级数据,存储类型有限
2.归档 - 大量数据,存储类型可以扩展到自定义对象
3.本地数据库 不管是属性列表还是归档,都是将数据保存到后台,前端任意一个界面都能访问
*/ - (void)viewDidLoad {
[super viewDidLoad];
/*
归档 - 本地化数据模型
<NSCoding> - 要对哪个类的对象进行归档,就让哪个类实现这个协议
*/
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeView)];
[self.view addGestureRecognizer:tap]; BWLoginDataModel *loginModel = [[BWLoginDataModel alloc] init];
loginModel.name = @"";
loginModel.password = @""; //沙盒路径 - 每一个应用都有自己的沙盒,IOS机制
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[] stringByAppendingPathComponent:@"account.data"];
//NSLog(@"%@",filePath); //将一个归档对象归档到目录下
if ([NSKeyedArchiver archiveRootObject:loginModel toFile:filePath]) {
NSLog(@"归档成功");
}
else
NSLog(@"归档失败"); } - (void)changeView
{
BWSecondViewController *secondVC = [[BWSecondViewController alloc] init]; [self presentViewController:secondVC animated:YES completion:nil];
} #pragma mark - 属性列表基础
- (void)useUserDefault
{
/*
//属性列表NSUserDefault(全局唯一,不用创建) 某些数据当存储本地之后,下次再次调用的时候,不用再次重新赋值,而是从本地直接获取
如果没有必要,别向属性列表存储大量数据
一般用来存储简单的全局都能使用到的数据
e.g. DeviceToken 设备令牌
SessionID 安全口令
ps:属性列表的存储和读取速度都是及时的,而归档和数据库都是有存储延时的
pss:自动登录
把用户的账号,密码存储到属性列表中 */
// NSString *str = @"bowen";
// [[NSUserDefaults standardUserDefaults] setObject:str forKey:@"sister"];
// NSString *newStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"sister"];
// NSLog(@"%@",newStr);
// NSLog(@"%@",[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]); //注意别删除错了
//[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"sister"];
//NSLog(@"%@",newStr); } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
 //
// BWSecondViewController.m
// IOS_0113_本地存储
//
// Created by ma c on 16/1/13.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import "BWSecondViewController.h"
#import "BWLoginDataModel.h" @interface BWSecondViewController () @end @implementation BWSecondViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor]; //解档
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[] stringByAppendingPathComponent:@"account.data"];
BWLoginDataModel *dataModel = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",dataModel);
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
 //
// BWLoginDataModel.h
// IOS_0113_本地存储
//
// Created by ma c on 16/1/13.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import <Foundation/Foundation.h> @interface BWLoginDataModel : NSObject<NSCoding> @property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *password; @end //
// BWLoginDataModel.m
// IOS_0113_本地存储
//
// Created by ma c on 16/1/13.
// Copyright (c) 2016年 博文科技. All rights reserved.
// #import "BWLoginDataModel.h" @implementation BWLoginDataModel - (NSString *)description
{
return [NSString stringWithFormat:@"name:%@ password:%@", self.name,self.password];
} #pragma mark - 归档方法
- (void)encodeWithCoder:(NSCoder *)aCoder
{
//按照规定的Key对属性进行编码操作
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.password forKey:@"password"]; }
#pragma mark - 解档方法
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
//归档和解档的key可以随意写,但他们属性对应的key必须相同
_name = [aDecoder decodeObjectForKey:@"name"];
_password = [aDecoder decodeObjectForKey:@"password"];
}
return self;
} @end

OC 数据持久化(数据本地化)- 本地存储的更多相关文章

  1. IOS开发--数据持久化篇之文件存储(一)

    前言:个人觉得开发人员最大的悲哀莫过于懂得使用却不明白其中的原理.在代码之前我觉得还是有必要简单阐述下相关的一些知识点. 因为文章或深或浅总有适合的人群.若有朋友发现了其中不正确的观点还望多多指出,不 ...

  2. RAC环境下误操作将数据文件添加到本地存储

    今天碰到个有意思的事情,有客户在Oracle RAC环境,误操作将新增的数据文件直接创建到了其中一个节点的本地存储上. 发现网上去搜的话这种问题还真不少,对应解决方案也各式各样,客户问我选择哪种方案可 ...

  3. vuex如何实现数据持久化,刷新页面存储的值还存在

    1.安装: npm install vuex-persistedstate --save 2.找到store/index.js import Vue from 'vue' import Vuex fr ...

  4. VueX数据持久化

    解决:Vue刷新时获取不到数据 解决方案:1.本地存储 2.Vuex数据持久化工具插件 本地存储 import Vue from "vue"; import Vuex from & ...

  5. React Native 之 数据持久化

    前言 因为 实战项目系列 涉及到数据持久化,这边就来补充一下. 如本文有错或理解偏差欢迎联系我,会尽快改正更新! 如有什么问题,也可直接通过邮箱 277511806@qq.com 联系我. demo链 ...

  6. scrapy 学习笔记2 数据持久化

    前情提要:校花网爬取,并进行数据持久化 数据持久化操作 --编码流程: 1:数据解析 2:封装item 类 3: 将解析的数据存储到实例化好的item 对象中 4:提交item 5:管道接收item然 ...

  7. React-Native 之 GD (十三)数据持久化(realm) 及 公共Cell

    1.数据持久化 数据持久化是移动端的一个重要部分,刚发现 Realm 原来已经支持 React-Native 了 步骤一: 引入 realm $ npm install realm --save 步骤 ...

  8. Spring Cloud Alibaba基础教程:Nacos的数据持久化

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

  9. IOS数据持久化之归档NSKeyedArchiver, NSUserDefaults,writeToFile

    //2.文件读写 //支持:NSString, NSArray , NSDictionay, NSData //注:集合(NSArray, NSDictionay)中得元素也必须是这四种类型, 才能够 ...

  10. redis启动加载过程、数据持久化

    背景 公司一年的部分业务数据放在redis服务器上,但数据量比较大,单纯的string类型数据一年就将近32G,而且是经过压缩后的. 所以我在想能否通过获取string数据的时间改为保存list数据类 ...

随机推荐

  1. js 中 0 和 null 、"" Boolean 值关系

    在做字符串非空判断时,无意发现一个问题,记录下以便以后回顾. 问题描述:非空判断,只是校验传值的内容是否为"".null .undefined.当变量 赋值的字符串内容为 0,此时 ...

  2. Python开发【笔记】: __get__和__getattr__和__getattribute__区别

    引言: 1.object.__getattr__(self, name) 当一般位置找不到attribute的时候,会调用getattr,返回一个值或AttributeError异常. 2.objec ...

  3. day08:软件系统的体系结构&Tomcat详解&Web应用&http协议

        day08 软件系统体系结构     常见软件系统体系结构B/S.C/S 1.1 C/S C/S结构即客户端/服务器(Client/Server),例如QQ: 需要编写服务器端程序,以及客户端 ...

  4. chrome 调试 ios的 H5 页面

    原文地址http://www.cnblogs.com/kelsen/p/6402477.html 本文重点讨论如何在 Windows 系统中通过chrome 浏览器调试运行在 iPhone Safar ...

  5. Linq to Sharepoint--如何获取Linq Query 生成的CALM

    我们知道Linq to sharepoint 实际最终还是转化成了CALM来对Sharepoint进行访问,那么我们怎样才能知道我们编写的Query语句最终转化成的CALM语句是什么样子呢. 我们可以 ...

  6. [LeetCode]206. Reverse Linked List(链表反转)

    Reverse a singly linked list. click to show more hints. Subscribe to see which companies asked this ...

  7. Entity Framework 复杂类型(转)

    为了说明什么是复杂属性,先举一个例子. public class CompanyAddress { public int ID { get; set; } public string CompanyN ...

  8. 浅谈location对象

    简介 Location 对象存储在 Window 对象的 Location 属性中,表示那个窗口中当前显示的文档的 Web 地址.通过Location对象,可以获取URL中的各项信息,调用对象方法也可 ...

  9. [one day one question] 有没有免费接收短信验证用于注册的软件或者平台?

    问题描述: 想要批量注册撸羊毛,有手机短信验证码验证,这怎么破? 解决方案: 免费的肯定没有的,不过"一条短信收费一毛钱"倒是有一个,本人是亲自试用过,该平台收不到短信验证码不收费 ...

  10. 面试官问:JS的this指向

    前言 面试官出很多考题,基本都会变着方式来考察this指向,看候选人对JS基础知识是否扎实.读者可以先拉到底部看总结,再谷歌(或各技术平台)搜索几篇类似文章,看笔者写的文章和别人有什么不同(欢迎在评论 ...