在日常开发中经常要对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. postman(动态数据获取)

    一:返回报文为 json 格式 示例:因为充值记录接口中需要用到登录接口返回报文中的信息如下 1.以获取token(JWT)和uid为例 2.在登录接口的tests中写入代码(因为登录接口报文信息中有 ...

  2. python-os.rmdir与shutil.rmtree的区别和用法

    每次写脚本的时候,pycharm都会自动生成缓存文件__pycache__文件,在提交代码的时候还得挨个删除,于是自己写一小段代码自动循环删除此目录及下面的文件. 思路: 先将目录及其下的文件读取出来 ...

  3. undef用法

    #undef的语法 定义:#undef 标识符,用来将前面定义的宏标识符取消定义. 整理了如下几种#undef的常见用法. 1. 防止宏定义冲突在一个程序块中用完宏定义后,为防止后面标识符冲突需要取消 ...

  4. D. Beautiful Array DP

    https://codeforces.com/contest/1155/problem/D 这个题目还是不会写,挺难的,最后还是lj大佬教我的. 这个题目就是要分成三段来考虑, 第一段就是不进行乘,就 ...

  5. Python拆分一列为多列

    有的员工,没有公司开户行的银行卡,发放现金工资.有时人多,需要计算币数.现金工资表中,其中一列为实发工资,import pandas as pd,转化为pd.DataFrame. 面值[100,50, ...

  6. JDBC04 PreparedStatement

    PreparedStatement类 存在预编译,用占位符去填参数(参数索引从1开始算),可以防止SQL注入 try { Class.forName("com.mysql.cj.jdbc.D ...

  7. [CodeForces 300D Painting Square]DP

    http://codeforces.com/problemset/problem/300/D 题意:每一次操作可以选一个正方形,令边长为n,如果n为奇数那么可以从中间画一个十字,分成4个大小相等的边长 ...

  8. FZU2105 线段树 (按位操作)

    题目: Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: (元素和操作元素 < 16) Opera ...

  9. PHP根据抖音的分享链接来抓包抖音视频

    现在抖音是个很火的短视频平台,上面有许多不错的小视频.今天教大家怎么用PHP技术来获取到抖音上的的内容. 1:打开抖音选中你认为好的视频点击分享,复制链接,然后你会获取到如下的内容: #科比 愿你去的 ...

  10. Web_python_template_injection

    0x01 pthon模板注入 判断是否为模板注入 paload http://124.126.19.106:34164/{{1+1}} //如果里面的值被执行了,那么存在模板注入 //调用os模块的p ...