iOS 数据持久化一-属性列表 Plist
来源:互联网 发布:java 线程等待和唤醒 编辑:程序博客网 时间:2024/05/21 10:31
iOS中有五种持久化数据的方式:属性列表、对象归档、NSUserDefault、SQLite3和Core Data
本文章讲述通过属性列表的方式持久化数据,这个方法也是我们平时最经常用到的方式。比如应用程序的配置和个性化的设置,一般都是通过属性列表(properties list) plist文件来存储和读取的。
- FOUNDATION_EXPORT NSArray *NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde);
这个函数的作用:创建一个目录搜索路径的列表。
创建一个路径字符串列表指定域中指定的目录。列表中的顺序,你应该搜索目录。
PS:
通过此方法返回的目录可能不存在。这种方法只是给你请求目录的适当位置。根据应用的需求,它可能是开发者创建合适的目录或者在任何两者之间。
下面是一个把数据以plist的形式保存到本地的自定义类- //
- // Plist.h
- // PlistDemo
- //
- // Created by swplzj on 13-11-20.
- // Copyright (c) 2013年 swplzj. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface Plist : NSObject
- - (void)modifyData:(NSString *)dataKey Value:(NSString *)dataValue;
- - (void)deleteDataWithDataKey:(NSString *)key;
- @end
- //
- // Plist.m
- // PlistDemo
- //
- // Created by swplzj on 13-11-20.
- // Copyright (c) 2013年 swplzj. All rights reserved.
- //
- #import "Plist.h"
- @implementation Plist
- - (id)init
- {
- if (self = [super init]) {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- //获取完整路径
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSLog(@"homeDirectory = %@", documentsDirectory);
- NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"student.plist"];
- //判断是否已经创建文件
- if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
- NSLog(@"student.plist文件已经存在!");
- }else {
- //plist文件没有被创建
- NSMutableDictionary *rootDic = [[NSMutableDictionary alloc] init];
- NSMutableDictionary *subDic = [[NSMutableDictionary alloc] init];
- [rootDic setObject:subDic forKey:@"student"];
- [subDic release];
- //写入到文件
- [rootDic writeToFile:plistPath atomically:YES];
- [rootDic release];
- }
- }
- return self;
- }
- //把数据写入到plist文件中
- - (void)modifyData:(NSString *)dataKey Value:(NSString *)dataValue
- {
- //获取路径
- NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"student.plist"];
- NSMutableDictionary *rootDic = [[[NSMutableDictionary alloc] initWithContentsOfFile:path] mutableCopy];
- NSMutableDictionary *studentInfo = [rootDic objectForKey:@"student"];
- NSString *name = [studentInfo objectForKey:dataKey];
- name = dataValue;
- [studentInfo setValue:name forKey:dataKey];
- //写入到文件
- [rootDic writeToFile:path atomically:YES];
- NSLog(@"Key - Value: %@ - %@",dataKey, dataValue );
- [rootDic release];
- }
- //根据key来删除某一行的数据
- - (void)deleteDataWithDataKey:(NSString *)key
- {
- //获取路径
- NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"student.plist"];
- NSMutableDictionary *rootDic = [[[NSMutableDictionary alloc] initWithContentsOfFile:path] mutableCopy];
- NSMutableDictionary *studentInfo = [rootDic objectForKey:@"student"];
- [studentInfo removeObjectForKey:key];
- NSLog(@"Delete data key - value: %@ - %@", key, [studentInfo objectForKey:key]);
- [rootDic setValue:studentInfo forKey:@"student"];
- //写入到文件
- [rootDic writeToFile:path atomically:YES];
- [rootDic release];
- }
- @end
在RootViewController里面的代码
- //
- // RootViewController.m
- // PlistDemo
- //
- // Created by swplzj on 13-11-20.
- // Copyright (c) 2013年 swplzj. All rights reserved.
- //
- #import "RootViewController.h"
- #import "Plist.h"
- @interface RootViewController ()
- @property (retain, nonatomic) Plist *plistFile;
- @property (retain, nonatomic) IBOutlet UITextField *keyTF;
- @property (retain, nonatomic) IBOutlet UITextField *valueTF;
- @property (retain, nonatomic) IBOutlet UITextField *deleteKeyTF;
- @end
- @implementation RootViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- _plistFile = [[Plist alloc] init];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)addOrModifyBtnClicked:(id)sender {
- if (![_keyTF.text isEqualToString:@""] && ![_valueTF.text isEqualToString:@""]) {
- [_plistFile modifyData:_keyTF.text Value:_valueTF.text];
- }
- }
- - (IBAction)deleteBtnClicked:(id)sender {
- if (![_deleteKeyTF.text isEqualToString:@""]) {
- [_plistFile deleteDataWithDataKey:_deleteKeyTF.text];
- }
- }
- - (void)dealloc {
- [_plistFile release];
- [_keyTF release];
- [_valueTF release];
- [_deleteKeyTF release];
- [super dealloc];
- }
- @end
效果
前往文件夹:
/Users/issuser/Library/Application Support/iPhone Simulator/6.1/Applications/A40F6B2C-002A-4797-B501-3ABCBE59723A/Documents
plist文件中的内容:
好了,这就是通过属性列表plist文件来达到数据持久化的目的。点击这里可以下载plistDemo代码
0 0
- iOS 数据持久化一-属性列表 Plist
- ios学习(五):数据持久化之属性列表(.plist)
- 数据持久化之属性列表Plist
- iOS 数据持久化一-属性列表
- plist 属性列表 ---数据持久化方式之一
- iOS 数据持久化 plist
- iOS 数据持久化-----plist
- iOS plist 数据持久化
- IOS 数据持久化之属性列表
- IOS 数据持久化(一)—— plist 存储
- swift数据持久化之属性列表,解决plist写入失败的问题
- iOS开发-数据持久化之plist文件
- iOS 数据持久化之plist
- iOS 数据持久化 — Plist
- iOS数据持久化 归档 plist文件
- iOS数据持久化之属性列表和归档
- [IOS] 使用属性列表实现数据持久化
- iOS数据持久化——属性列表和归档
- C实现 LeetCode->Remove Duplicates from Sorted List (枚举法)(单链表去重复节点)
- grails转换base64
- iOS 数据持久化三-NSUserDefaults
- groovy构建和解析 XML
- AC自动机
- iOS 数据持久化一-属性列表 Plist
- Keepalived实现HA
- rtp协议详解/rtcp协议详解
- 【JavaScript】分秒倒计时器
- Lua 中 table 库函数 insert、remove、sort
- iOS 数据持久化二-对象归档
- grails、groovy创建xml
- 在查看源码时Eclipse提示source not found
- iOS 数据持久化四-SQLite3