iOS 数据持久化一-属性列表
来源:互联网 发布:windows 10名版本 编辑:程序博客网 时间:2024/06/06 05:34
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 数据持久化一-属性列表
- iOS 数据持久化一-属性列表 Plist
- IOS 数据持久化之属性列表
- ios学习(五):数据持久化之属性列表(.plist)
- iOS数据持久化之属性列表和归档
- [IOS] 使用属性列表实现数据持久化
- iOS数据持久化——属性列表和归档
- 《精通iOS开发·第六版》数据持久化基础知识之属性列表持久化
- iOS持久化存储之属性列表
- 数据持久化-属性列表、归档、NSUserDefaults
- 数据持久化之属性列表
- 数据持久化之属性列表Plist
- iOS-数据持久化(一)
- iOS 数据持久化(一)
- IOS 开发学习二十 数据持久化:读写属性列表示例
- IOS数据持久化之一属性列表(或偏好设置)
- IOS开发指南学习——数据持久化(属性列表、对象归档、SQLite)
- iphone开发中数据持久化之——属性列表序列化(一)
- javaSE/day1
- oAuth
- DES加密源码
- 去掉Android app上的Title Bar
- 李克强来到赤峰市考察
- iOS 数据持久化一-属性列表
- 编写aop:around时出现的异常org.springframework.aop.AopInvocationException:
- 利用 SCILAB 设计 iir 滤波器设计(模拟滤波器双线性变换法)
- 微秒时间的获取
- php 正则获取img所有属性值,包括src等,保存一个,我不懂正则啊
- HttpServletRequest,对象,的详解
- HDU 2124
- keypad 驱动分析
- m3图像显示不完整,左侧被拉伸