在日常开发中经常要对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. 别了,JavaScript;你好,Blazor

    Web开发与JavaScript开发向来是同义词.直到WebAssembly的横空出世,WebAssembly (Wasm)是一种在浏览器中可以执行的二进制指令. WebAssembly 的 官方工具 ...

  2. IP 基础知识全家桶,45 张图一套带走

    前言 前段时间,有读者希望我写一篇关于 IP 分类地址.子网划分等的文章,他反馈常常混淆,摸不着头脑. 那么,说来就来!而且要盘就盘全一点,顺便挑战下小林的图解功力,所以就来个 IP 基础知识全家桶. ...

  3. Android(H5)互相调用方法

    记录一下前面混合开发时很重要的java与js互调方法进行数据交互. 混合开发就需要webview这个控件了 这就很玄学了,哈哈哈 这篇文章https://www.jianshu.com/p/3d9a9 ...

  4. 基于胜率矩阵的PageRank排序

      在做博弈模型评估的时候,遇到一个问题是如何评价多个模型的优劣.例如我有训练好的三个围棋模型A,B,C,两两之间对打之后有一个胜负关系,如何对这三个模型进行排序呢?通常对于人类选手这种水平有波动的情 ...

  5. scala 中 Any、AnyRef、Object、AnyVal 关系

    Any,是 scala 中的抽象类,不能实例化 AnyRef 继承于 Any,它是一个 trait AnyVal 继承于 Any,它是一个抽象类,目的是消除基本类型,scala中只有引用类型,仅此作用 ...

  6. JS字符串截取 “指定字符” 前面和后面的内容!

    JS字符串截取 “指定字符” 前面和后面的内容! var string= "07/12" var before = string.split('/')[0] var after = ...

  7. docker虚拟化平台构建

    docker虚拟化平台构建 从1.13版本以后的docker软件分为连个版本:企业版.社区版,在企业中推荐社区版本. 构建docker平台环境,系统选择centos7.x,推荐linux内核3.10 ...

  8. ocelot jwt 进行统一验证

    前一个帖子发了有关jwt 验证api的内容,这一次将jwt集成到ocelot网关中. ocelot集成jwt有一个很不错的nuget包,ocelot.jwtauthorize  ,但是这个包似乎支持n ...

  9. hdu5381 The sum of gcd]莫队算法

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=5381 思路:这个题属于没有修改的区间查询问题,可以用莫队算法来做.首先预处理出每个点以它为起点向左和向右连 ...

  10. 配置类为什么要添加@Configuration注解呢?

    配置类为什么要添加@Configuration注解呢? 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 推荐阅读: Spring官网阅读 | 总结篇 Spring ...