在日常开发中经常要对NSString、NSDictionary、NSArray、NSData、NSNumber这些基本类的数据进行持久化,我们可以用XML属性列表持久化到.plist 文件中,也可以用NSUserDefaults,下面我们就开搞吧。

XML属性列表方式:

#import "AppDelegate.h"
#import "Person.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSDictionary *studentDic = [NSDictionary dictionaryWithObjectsAndKeys:@"SuperDo.Mount",@"student_mount",@"SuperDo.AC",@"student_ac",@"SuperDo.Horse",@"student_horse", nil]; //获得Document的路径
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documents stringByAppendingPathComponent:@"students.plist"]; //保存到Document目录下
if([studentDic writeToFile:path atomically:YES]) {
NSLog(@"Save to file success!");
} //从path初始化
NSDictionary *students = [NSDictionary dictionaryWithContentsOfFile:path];
for (NSString *key in students.allKeys) {
NSLog(@"%@: %@",key,[students objectForKey:key]);
} return YES;
} @end

保存到Documet下文件信息:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>student_ac</key>
<string>SuperDo.AC</string>
<key>student_horse</key>
<string>SuperDo.Horse</string>
<key>student_mount</key>
<string>SuperDo.Mount</string>
</dict>
</plist>

NSUserDefaults 方式:

#import "AppDelegate.h"
#import "Person.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSDictionary *studentDic = [NSDictionary dictionaryWithObjectsAndKeys:@"SuperDo.Mount",@"student_mount",@"SuperDo.AC",@"student_ac",@"SuperDo.Horse",@"student_horse", nil]; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:studentDic forKey:@"students"]; NSDictionary *dic = [userDefaults objectForKey:@"students"];
for (NSString *key in dic.allKeys) {
NSLog(@"NSUserDefaults 方式 ---- 》 %@: %@",key,[dic objectForKey:key]);
} return YES;
} @end

XML属性列表方式打印结果:

2015-07-06 20:39:53.623 Attendance[1129:68434] Save to file success!

2015-07-06 20:39:53.624 Attendance[1129:68434] student_ac: SuperDo.AC

2015-07-06 20:39:53.624 Attendance[1129:68434] student_mount: SuperDo.Mount

2015-07-06 20:39:53.624 Attendance[1129:68434] student_horse: SuperDo.Horse

NSUserDefault方式打印结果:

2015-07-06 20:50:44.743 Attendance[1155:70007] NSUserDefaults 方式 ---- student_mount: SuperDo.Mount

2015-07-06 20:50:44.743 Attendance[1155:70007] NSUserDefaults 方式 ---- student_ac: SuperDo.AC

2015-07-06 20:50:44.743 Attendance[1155:70007] NSUserDefaults 方式 ---- student_horse: SuperDo.Horse

本站文章为宝宝巴士 SD.Team原创,转载务必在明显处注明:(作者官方网站:宝宝巴士
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4625366.html

[Objective-C] 012_数据持久化_XML属性列表,NSUserDefaults的更多相关文章

  1. iphone开发中数据持久化之——属性列表序列化(一)

    数据持久化是应用程序开发过程中的一个基本问题,对应用程序中的数据进行持久化存储,有多重不同的形式.本系列文章将介绍在iphone开发过程中数据持久化的三种主要形式,分别是属性列表序列号.对象归档化以及 ...

  2. iOS数据存储之属性列表理解

    iOS数据存储之属性列表理解 数据存储简介 数据存储,即数据持久化,是指以何种方式保存应用程序的数据. 我的理解是,开发了一款应用之后,应用在内存中运行时会产生很多数据,这些数据在程序运行时和程序一起 ...

  3. 数据存储之属性列表Plist

    常用的数据存储有属性列表.偏好设置.归档.sqlite.coreData.上一博客了解了沙盒,现在了解下属性列表Plist. 通常通过NSArray.NSDictionary集合类的WriteToFi ...

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

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

  5. objective C中数据持久化方式1--对象归档

    第一.数据持久化的方式: NSKeyedArchiver--对象归档 属性列表化(NSArray.NSDictionary.NSUserDefault) SQlite数据库.CoreData数据库 其 ...

  6. iOS 数据持久性存储-属性列表

    iOS上常用四种数据存取方法有: 1.属性列表 2.对象归档 3.iOS的嵌入式关系数据库(SQLite3) 4.苹果公司提供持久性共聚Core Data 由于苹果公司的沙盒机制,每个应用程序都有自己 ...

  7. iOS数据持久化--用户属性

    一.简介 NSUserDefaults类是一个单例类,每个程序只有一个 NSUserDefaults对象,可以用来存储用户的属性,比如自动登录时候的账号密码等小型的数据. 二.使用 1.NSUserD ...

  8. 1211笔记关于//modal//更改窗口的根控制器//数据存取//Plist属性列表//-“沙盒机制”//plis属性列表//偏好设置//归档普通对象//联系人数据存储//协议与回调函数

    一.利用Modal形式展示控制器 1.如何展示// vc就是要展示的新控制器[self presentViewController:vc animated:YES completion:^{    N ...

  9. iOS数据持久化

    在iOS中,实现数据持久化一般分为4大种: 1.属性列表 2.对象归档 3.SQLite 4.Core Data 一.属性列表 NSUserDefaults类的使用和NSKeyedArchiver有很 ...

随机推荐

  1. cmake安装jsoncpp

    cd jsoncpp- mkdir -p build/debug cd build/debug cmake -DCMAKE_BUILD_TYPE=release -DBUILD_STATIC_LIBS ...

  2. 全面解读 vue3.0

    在此附上链接,在知乎上看到的,感觉说的很详细 https://zhuanlan.zhihu.com/p/46269528

  3. R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数

    R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...

  4. P4016 负载平衡问题 网络流重温

    P4016 负载平衡问题 这个题目现在第二次做,感觉没有这么简单,可能是我太久没有写这种题目了,基本上都忘记了,所以我连这个是费用流都没有看出来. 有点小伤心,知道是费用流之后,我居然还拆点了. 这个 ...

  5. Spring官网阅读(四)BeanDefinition(上)

    前面几篇文章已经学习了官网中的1.2,1.3,1.4三小结,主要是容器,Bean的实例化及Bean之间的依赖关系等.这篇文章,我们继续官网的学习,主要是BeanDefinition的相关知识,这是Sp ...

  6. leetcode_雇佣 K 名工人的最低成本(优先级队列,堆排序)

    题干: 有 N 名工人. 第 i 名工人的工作质量为 quality[i] ,其最低期望工资为 wage[i] . 现在我们想雇佣 K 名工人组成一个工资组.在雇佣 一组 K 名工人时,我们必须按照下 ...

  7. imos-累积和法

    在解AOJ 0531 Paint Color时,学到了一个累积和的妙用--imos法,由于原文是日语,所以特意翻译过来.值得一提的是,作者Kentaro Imajo跟鄙人同龄,却已取得如此多的成就,而 ...

  8. springboot+vue前后端免费开源

    序言 继上一篇 一套管理系统基础模版 详细梳理一下安装流程,功能说明,开发规范等. 后端项目结构? 如何从零搭建环境开发? 如何打包部署? 接入开发及规范 项目地址 小结 后端项目结构 ​ shop- ...

  9. (Python基础教程之八)Python中的list操作

    Python基础教程 在SublimeEditor中配置Python环境 Python代码中添加注释 Python中的变量的使用 Python中的数据类型 Python中的关键字 Python字符串操 ...

  10. PAT 1001 A+B Format (20分) to_string()

    题目 Calculate a+b and output the sum in standard format -- that is, the digits must be separated into ...