iOS中有五種持久化數據的方式:屬性列表、對象歸檔、NSUserDefault、SQLite3和Core Data
本文章講述通過屬性列表的方式持久化數據,這個方法也是我們平時最經常用到的方式。比如應用程序的配置和個性化的設置,一般都是通過屬性列表(properties list) plist文件來存儲和讀取的。 [objc] FOUNDATION_EXPORT NSArray *NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde); 這個函數的作用:創建一個目錄搜索路徑的列表。 創建一個路徑字符串列表指定域中指定的目錄。列表中的順序,你應該搜索目錄。 PS: 通過此方法返回的目錄可能不存在。這種方法只是給你請求目錄的適當位置。根據應用的需求,它可能是開發者創建合適的目錄或者在任何兩者之間。 下面是一個把數據以plist的形式保存到本地的自定義類 [objc] // // 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 [objc] // // 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裡面的代碼 [objc] // // 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