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数据类 ...
随机推荐
- This module embeds Lua, via LuaJIT 2.0/2.1, into Nginx and by leveraging Nginx's subrequests, allows the integration of the powerful Lua threads (Lua coroutines) into the Nginx event model.
openresty/lua-nginx-module: Embed the Power of Lua into NGINX HTTP servers https://github.com/openre ...
- BBS - 文章评论
一.文章评论 <div class="comment_region"> <div class="row"> <div class= ...
- 利用阿里云搭建私有Git服务器
服务器系统:Centos 6 (查看centos版本命令:lsb_release -a) 客户端系统:Windows 7 一.服务器端安装Git ==通常centos上使用yum源安装的git版本过低 ...
- (2.13)Mysql之SQL基础——触发器
(2.13)Mysql之SQL基础——触发器 关键词:Mysql触发器 1.一般形式 -- 0.查看触发器[1]SHOW TRIGGERS;[2]SELECT * FROM `information_ ...
- umlの交互图
版权声明:本文为博主原创文章,若要转载请注明出处!^_^ https://blog.csdn.net/u010892841/article/details/24920155 前面介绍了uml的非常多种 ...
- fold change的意义[转载]
转自:https://zhidao.baidu.com/question/2052933434631672387.html 1.解释 解释:表达值倍数变化 ,分析,消除可能的混杂因素,必要时可以用读段 ...
- PAT 1043 Is It a Binary Search Tree[二叉树][难]
1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...
- c++11 类默认函数的控制:"=default" 和 "=delete"函数 void fun() = default; void fun()=delete;
转自:lsgxeva #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #includ ...
- 1linux的基本命令
查看命令的帮助信息man 命令名 文件操作touch 建立文件 (对于已存在文件,更新时间)cat 查看文件 (-n 自动加上行号)rm 删除文件cp 拷贝文件mv 移动/重命名文件more 分页查看 ...
- 一、怎样使用eclipse查看JDK源码
前言: JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JAVA基础的类库.阅读一些系统的源码会帮助你理解一些基本的原理. 一.创建一个工程 在eclipse中创建一个jav ...