OC 数据持久化(数据本地化)- 本地存储
//
// 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 数据持久化(数据本地化)- 本地存储的更多相关文章
- IOS开发--数据持久化篇之文件存储(一)
前言:个人觉得开发人员最大的悲哀莫过于懂得使用却不明白其中的原理.在代码之前我觉得还是有必要简单阐述下相关的一些知识点. 因为文章或深或浅总有适合的人群.若有朋友发现了其中不正确的观点还望多多指出,不 ...
- RAC环境下误操作将数据文件添加到本地存储
今天碰到个有意思的事情,有客户在Oracle RAC环境,误操作将新增的数据文件直接创建到了其中一个节点的本地存储上. 发现网上去搜的话这种问题还真不少,对应解决方案也各式各样,客户问我选择哪种方案可 ...
- vuex如何实现数据持久化,刷新页面存储的值还存在
1.安装: npm install vuex-persistedstate --save 2.找到store/index.js import Vue from 'vue' import Vuex fr ...
- VueX数据持久化
解决:Vue刷新时获取不到数据 解决方案:1.本地存储 2.Vuex数据持久化工具插件 本地存储 import Vue from "vue"; import Vuex from & ...
- React Native 之 数据持久化
前言 因为 实战项目系列 涉及到数据持久化,这边就来补充一下. 如本文有错或理解偏差欢迎联系我,会尽快改正更新! 如有什么问题,也可直接通过邮箱 277511806@qq.com 联系我. demo链 ...
- scrapy 学习笔记2 数据持久化
前情提要:校花网爬取,并进行数据持久化 数据持久化操作 --编码流程: 1:数据解析 2:封装item 类 3: 将解析的数据存储到实例化好的item 对象中 4:提交item 5:管道接收item然 ...
- React-Native 之 GD (十三)数据持久化(realm) 及 公共Cell
1.数据持久化 数据持久化是移动端的一个重要部分,刚发现 Realm 原来已经支持 React-Native 了 步骤一: 引入 realm $ npm install realm --save 步骤 ...
- Spring Cloud Alibaba基础教程:Nacos的数据持久化
前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...
- IOS数据持久化之归档NSKeyedArchiver, NSUserDefaults,writeToFile
//2.文件读写 //支持:NSString, NSArray , NSDictionay, NSData //注:集合(NSArray, NSDictionay)中得元素也必须是这四种类型, 才能够 ...
- redis启动加载过程、数据持久化
背景 公司一年的部分业务数据放在redis服务器上,但数据量比较大,单纯的string类型数据一年就将近32G,而且是经过压缩后的. 所以我在想能否通过获取string数据的时间改为保存list数据类 ...
随机推荐
- 005-线程sleep、join、yield、wait、notify、notifyAll、run、start、synchronized
一.线程sleep join yield wait 1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchroni ...
- xmr monero miner
https://github.com/fireice-uk/xmr-stak-cpu xmr-stak-cpu安装 xmr-stak-cpu是一个用于cpu计算的开源软件,下面记录在ubuntu17. ...
- Mybatis入门和简单Demo
一.Mybatis的诞生 回顾下传统的数据库开发,JDBC和Hibernate是使用最普遍的技术,但这两种ORM框架都存在一定的局限性: JDBC:最原生的技术,简单易学,执行速度快,效率高,适合大数 ...
- file /etc/httpd/conf.d/php.conf from install of php-5.6.37-1.el7.remi.x86_64 conflicts with file from package mod_php71w-7.1.18-1.w7.x86_64
yum remove mod_php71w php71w-cli
- python3 捕捉代码行出错的小程序
下面主要使用的是: try .. except .. else .. finally ...,用的是 traceback 这个第三方模块. import traceback #捕捉哪行代码报错 def ...
- chrome 调试 ios的 H5 页面
原文地址http://www.cnblogs.com/kelsen/p/6402477.html 本文重点讨论如何在 Windows 系统中通过chrome 浏览器调试运行在 iPhone Safar ...
- mysql慢日志
mysql慢日志是用来记录执行时间比较长的sql工具(超过long_query_time的sql),这样对于跟踪有问题的sql很有帮助. 查看是否启用慢日志和相关信息 上面截图其中: log_slow ...
- 《Clean Code》一书回顾
<Clean Code>一书从翻开至今,已经差不多两个月的时间了,尽管刨去其中的假期,算下来实在是读得有点慢.阅读期间,断断续续的做了不少笔记.之前,每每在读完了一本技术书籍之后,其中的诸 ...
- flask使用ajax提交表单
Flask中使用ajax提交表单刷新数据,避免提交表单后使用return render_temp()会刷新页面 <form id ="test_form"> {{ fo ...
- javascript日期字符串和日期对象相互转换
HTML页面间需要传递日期和时间参数的时候,如果需要对日期字符串进行时间的运算,就需要先将日期字符串转换成JS日期对象. 在js中,yyyy-MM-dd HH:mm:ss格式的日期字符串不能用来直接构 ...