當前位置:成語大全網 - 新華字典 - 如何對.plist文件添加,刪除數據

如何對.plist文件添加,刪除數據

主要操作:

1.//獲得plist路徑 -(NSString*)getPlistPath;

2.//判斷沙盒中名為plistname的文件是否存在 -(BOOL) isPlistFileExists;

3.//讀取沙盒中Document文件夾下的BookList.plist文件

[NSMutableDictionarydictionaryWithContentsOfFile:plistPath];

4.//寫入文件 if ([plistDictionary writeToFile:plistPath atomically:YES])

WBBooksManager.m文件:

#import "WBBooksManager.h"

@implementation WBBooksManager

static WBBooksManager *g_instance = nil;

+ (WBBooksManager *)sharedInstance

{

@synchronized(self) {

if ( g_instance == nil ) {

g_instance = [[self alloc] init];

}

}

return g_instance;

}

//獲得plist路徑

-(NSString*)getPlistPath{

//沙盒中的文件路徑

NSArray *storeFilePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *doucumentsDirectiory = [storeFilePath objectAtIndex:0];

NSString *plistPath =[doucumentsDirectiory stringByAppendingPathComponent:@"WBBooks.plist"]; //根據需要更改文件名

return plistPath;

}

//判斷沙盒中名為plistname的文件是否存在

-(BOOL) isPlistFileExists{

NSString *plistPath =[[WBBooksManager sharedInstance]getPlistPath];

NSFileManager *fileManager = [NSFileManager defaultManager];

if( [fileManager fileExistsAtPath:plistPath]== NO ) {

NSLog(@"not exists");

return NO;

}else{

return YES;

}

}

-(void)initPlist{

NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];

//如果plist文件不存在,將工程中已建起的plist文件寫入沙盒中

if (! [[WBBooksManager sharedInstance] isPlistFileExists]) {

//從自己建立的plist文件 復制到沙盒中 ,方法壹

NSError *error;

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *bundle = [[NSBundle mainBundle] pathForResource:@"WBBooks" ofType:@"plist"];

[fileManager copyItemAtPath:bundle toPath:plistPath error:&error];

//方法二

// NSString *path = [[NSBundle mainBundle] pathForResource:@"WBBooks"ofType:@"plist"];

// NSMutableDictionary *activityDics = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

// [activityDics writeToFile:plistPath atomically:YES];

}

}

//判斷key的書是否存在

-(BOOL)isBookExistsForKey:(NSString*)key{

NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];

NSMutableDictionary *WBBooksDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

//根目錄下存在名為bookname的字典

if ([WBBooksDictionary objectForKey:key]) {

return YES;

}else{

return NO;

}

}

//根據key值刪除對應書籍

-(void)removeBookWithKey:(NSString *)key{

NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];

NSMutableDictionary *WBBooksDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

[WBBooksDictionary removeObjectForKey:key];

[WBBooksDictionary writeToFile:plistPath atomically:YES]; //刪除後重新寫入

}

//刪除plistPath路徑對應的文件

-(void)deletePlist{

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];

[fileManager removeItemAtPath:plistPath error:nil];

}

//將dictionary寫入plist文件,前提:dictionary已經準備好

-(void)writePlist:(NSMutableDictionary*)dictionary forKey:(NSString *)key{

NSMutableDictionary *plistDictionary = [[NSMutableDictionary alloc]init];

//如果已存在則讀取現有數據

if ([[WBBooksManager sharedInstance]isPlistFileExists]) {

plistDictionary = [[WBBooksManager sharedInstance]readPlist];

}

//增加壹個數據

[plistDictionary setValue:dictionary forKey:key]; //在plistDictionary增加壹個key為...的value

NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];

if([plistDictionary writeToFile:plistPath atomically:YES]){

NSLog(@"write ok!");

}else{

NSLog(@"ddd");

}

}

//

-(NSMutableDictionary*)readPlist{

NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];

NSMutableDictionary *resultDictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];

return resultDictionary;

}

//讀取plist文件內容復制給dictionary 備用

-(void)readPlist:(NSMutableDictionary **)dictionary{

NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath];

*dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

}

//更改壹條數據,就是把dictionary內key重寫

-(void)replaceDictionary:(NSMutableDictionary *)newDictionary withDictionaryKey:(NSString *)key{

[[WBBooksManager sharedInstance]removeBookWithKey:key];

[[WBBooksManager sharedInstance]writePlist:newDictionary forKey:key];

}

-(NSInteger)getBooksCount{

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

dictionary = [[WBBooksManager sharedInstance] readPlist];

return [dictionary count];

}