iOS 数据持久化一-属性列表 Plist

来源:互联网 发布:java 线程等待和唤醒 编辑:程序博客网 时间:2024/05/21 10:31

iOS中有五种持久化数据的方式:属性列表、对象归档、NSUserDefault、SQLite3和Core Data

本文章讲述通过属性列表的方式持久化数据,这个方法也是我们平时最经常用到的方式。比如应用程序的配置和个性化的设置,一般都是通过属性列表(properties list) plist文件来存储和读取的。


[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. FOUNDATION_EXPORT NSArray *NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde);  

这个函数的作用:创建一个目录搜索路径的列表。

创建一个路径字符串列表指定域中指定的目录。列表中的顺序,你应该搜索目录。

PS:

通过此方法返回的目录可能不存在。这种方法只是给你请求目录的适当位置。根据应用的需求,它可能是开发者创建合适的目录或者在任何两者之间。

下面是一个把数据以plist的形式保存到本地的自定义类

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  Plist.h  
  3. //  PlistDemo  
  4. //  
  5. //  Created by swplzj on 13-11-20.  
  6. //  Copyright (c) 2013年 swplzj. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Plist : NSObject  
  12.   
  13. - (void)modifyData:(NSString *)dataKey Value:(NSString *)dataValue;  
  14. - (void)deleteDataWithDataKey:(NSString *)key;  
  15.   
  16. @end  

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  Plist.m  
  3. //  PlistDemo  
  4. //  
  5. //  Created by swplzj on 13-11-20.  
  6. //  Copyright (c) 2013年 swplzj. All rights reserved.  
  7. //  
  8.   
  9. #import "Plist.h"  
  10.   
  11. @implementation Plist  
  12.   
  13. - (id)init  
  14. {  
  15.     if (self = [super init]) {  
  16.         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  17.         //获取完整路径  
  18.         NSString *documentsDirectory = [paths objectAtIndex:0];  
  19.         NSLog(@"homeDirectory = %@", documentsDirectory);  
  20.         NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"student.plist"];  
  21.         //判断是否已经创建文件  
  22.         if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {  
  23.             NSLog(@"student.plist文件已经存在!");  
  24.         }else {  
  25.             //plist文件没有被创建  
  26.             NSMutableDictionary *rootDic = [[NSMutableDictionary alloc] init];  
  27.             NSMutableDictionary *subDic = [[NSMutableDictionary alloc] init];  
  28.             [rootDic setObject:subDic forKey:@"student"];  
  29.             [subDic release];  
  30.             //写入到文件  
  31.             [rootDic writeToFile:plistPath atomically:YES];  
  32.             [rootDic release];  
  33.         }  
  34.     }  
  35.       
  36.     return self;  
  37. }  
  38.   
  39. //把数据写入到plist文件中  
  40. - (void)modifyData:(NSString *)dataKey Value:(NSString *)dataValue  
  41. {  
  42.     //获取路径  
  43.     NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"student.plist"];  
  44.     NSMutableDictionary *rootDic = [[[NSMutableDictionary alloc] initWithContentsOfFile:path] mutableCopy];  
  45.     NSMutableDictionary *studentInfo = [rootDic objectForKey:@"student"];  
  46.     NSString *name = [studentInfo objectForKey:dataKey];  
  47.     name = dataValue;  
  48.     [studentInfo setValue:name forKey:dataKey];  
  49.     //写入到文件  
  50.     [rootDic writeToFile:path atomically:YES];  
  51.     NSLog(@"Key - Value: %@ - %@",dataKey, dataValue );  
  52.     [rootDic release];  
  53. }  
  54.   
  55. //根据key来删除某一行的数据  
  56. - (void)deleteDataWithDataKey:(NSString *)key  
  57. {  
  58.     //获取路径  
  59.     NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"student.plist"];  
  60.     NSMutableDictionary *rootDic = [[[NSMutableDictionary alloc] initWithContentsOfFile:path] mutableCopy];  
  61.     NSMutableDictionary *studentInfo = [rootDic objectForKey:@"student"];  
  62.     [studentInfo removeObjectForKey:key];  
  63.     NSLog(@"Delete data key - value: %@ - %@", key, [studentInfo objectForKey:key]);  
  64.     [rootDic setValue:studentInfo forKey:@"student"];  
  65.     //写入到文件  
  66.     [rootDic writeToFile:path atomically:YES];  
  67.     [rootDic release];  
  68. }  
  69.   
  70. @end  

在RootViewController里面的代码

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  RootViewController.m  
  3. //  PlistDemo  
  4. //  
  5. //  Created by swplzj on 13-11-20.  
  6. //  Copyright (c) 2013年 swplzj. All rights reserved.  
  7. //  
  8.   
  9. #import "RootViewController.h"  
  10. #import "Plist.h"  
  11.   
  12. @interface RootViewController ()  
  13.   
  14. @property (retainnonatomicPlist *plistFile;  
  15.   
  16. @property (retainnonatomic) IBOutlet UITextField *keyTF;  
  17. @property (retainnonatomic) IBOutlet UITextField *valueTF;  
  18. @property (retainnonatomic) IBOutlet UITextField *deleteKeyTF;  
  19. @end  
  20.   
  21. @implementation RootViewController  
  22.   
  23. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  24. {  
  25.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  26.     if (self) {  
  27.         // Custom initialization  
  28.     }  
  29.     return self;  
  30. }  
  31.   
  32. - (void)viewDidLoad  
  33. {  
  34.     [super viewDidLoad];  
  35.     // Do any additional setup after loading the view from its nib.  
  36.     _plistFile = [[Plist alloc] init];  
  37. }  
  38.   
  39. - (void)didReceiveMemoryWarning  
  40. {  
  41.     [super didReceiveMemoryWarning];  
  42.     // Dispose of any resources that can be recreated.  
  43. }  
  44.   
  45. - (IBAction)addOrModifyBtnClicked:(id)sender {  
  46.     if (![_keyTF.text isEqualToString:@""] && ![_valueTF.text isEqualToString:@""]) {  
  47.         [_plistFile modifyData:_keyTF.text Value:_valueTF.text];   
  48.     }  
  49. }  
  50.   
  51. - (IBAction)deleteBtnClicked:(id)sender {  
  52.     if (![_deleteKeyTF.text isEqualToString:@""]) {  
  53.         [_plistFile deleteDataWithDataKey:_deleteKeyTF.text];  
  54.     }  
  55. }  
  56.   
  57. - (void)dealloc {  
  58.     [_plistFile release];  
  59.     [_keyTF release];  
  60.     [_valueTF release];  
  61.     [_deleteKeyTF release];  
  62.     [super dealloc];  
  63. }  
  64. @end  

效果


前往文件夹:

/Users/issuser/Library/Application Support/iPhone Simulator/6.1/Applications/A40F6B2C-002A-4797-B501-3ABCBE59723A/Documents





plist文件中的内容:




好了,这就是通过属性列表plist文件来达到数据持久化的目的。点击这里可以下载plistDemo代码
0 0
原创粉丝点击