Plist文件增删查改

 

主要操作:

1.//获得plist路径    -(NSString*)getPlistPath;

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

3.//读取沙盒中Document文件夹下的BookList.plist文件

[NSMutableDictionarydictionaryWithContentsOfFile:plistPath];

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

 #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:];
     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];

 }

//导入头文件
#import "WBBooksManager.h"

-(void)viewDidLoad中添加以下代码,是刚写的时候测试时用的,狠时繁杂。 将就着看好了、

    WBBooksManager *sss = [[WBBooksManager alloc] init ];

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

       if( [sss isPlistFileExists]== NO ) {//不存在

           NSLog(@"WBBooks.plist not exists ,build it.");

          NSMutableDictionary *addDictionary1 = [[NSMutableDictionary alloc] init];
          NSString *addName1 = [NSString stringWithFormat:@"www"];
          NSNumber *addNumber1 = [[NSNumber alloc] initWithInt:];
          [addDictionary1 setValue:addName1 forKey:@"name"];
          [addDictionary1 setValue:addNumber1 forKey:@"list"];
          [[WBBooksManager sharedInstance]writePlist:addDictionary1 forKey:@"Add1"];

          NSMutableDictionary *addDictionary2 = [[NSMutableDictionary alloc] init];
          NSString *addName2 = [NSString stringWithFormat:@"aaas"];
          NSNumber *addNumber2 = [[NSNumber alloc] initWithInt:];
          [addDictionary2 setValue:addName2 forKey:@"name"];
          [addDictionary2 setValue:addNumber2 forKey:@"list"];
          [[WBBooksManager sharedInstance]writePlist:addDictionary2 forKey:@"Add2"];

      }

      NSMutableDictionary *resultDictionary = [[NSMutableDictionary alloc] init];
      [[WBBooksManager sharedInstance] readPlist:&resultDictionary];
      NSLog(@"add1的结果%@", resultDictionary);   

      NSArray *array = [resultDictionary allKeys];  //所有的Book

      int num =  [[WBBooksManager sharedInstance] getBooksCount]; //总数
      NSLog(@"array%@ %d",array,num);

      NSMutableDictionary *plistDictionary = [[NSMutableDictionary alloc]init];
      plistDictionary = [[WBBooksManager sharedInstance]readPlist];

      NSMutableDictionary *addDictionary2 = [[NSMutableDictionary alloc] init];
      NSString *addName2 = [NSString stringWithFormat:@"dafd"];
      NSNumber *addNumber2 = [[NSNumber alloc] initWithInt:];
      [addDictionary2 setValue:addName2 forKey:@"name"];
      [addDictionary2 setValue:addNumber2 forKey:@"list"];

      [plistDictionary setValue:addDictionary2 forKey:@"Add2"];
      [plistDictionary writeToFile:plistPath atomically:YES];

      resultDictionary = [[NSMutableDictionary alloc] init];
      [[WBBooksManager sharedInstance] readPlist:&resultDictionary];
      NSLog(@"add1的结果%@", resultDictionary);   

      array = [resultDictionary allKeys];  //所有的Book

      num =  [[WBBooksManager sharedInstance] getBooksCount]; //总数
      NSLog(@"array%@ %d",array,num);

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

      NSString *addName1 = [NSString stringWithFormat:@"wmmm"];
      NSNumber *addNumber1 = [[NSNumber alloc] initWithInt:];
      [addDictionary1 setValue:addName1 forKey:@"name"];
      [addDictionary1 setValue:addNumber1 forKey:@"list"];

      //判断给出的Key对应的数据是否存在
      if ([[WBBooksManager sharedInstance] isBookExistsForKey:@"Add1"]) {
          //存在,则替换之
          NSLog(@"存在,则替换之");
          [[WBBooksManager sharedInstance] replaceDictionary:addDictionary1 withDictionaryKey:@"Add1"];
      }else{//不存在,则写入
          NSLog(@"不存在,则写入");
          [[WBBooksManager sharedInstance] writePlist:addDictionary1 forKey:@"Add1"];
      }

      resultDictionary = [[NSMutableDictionary alloc] init];
      [[WBBooksManager sharedInstance] readPlist:&resultDictionary];
      NSLog(@"add1的结果%@", resultDictionary);   

      array = [resultDictionary allKeys];  //所有的Book

      num =  [[WBBooksManager sharedInstance] getBooksCount]; //总数
      NSLog(@"array%@ %d",array,num);

      addName1 = [NSString stringWithFormat:@"wmmm"];
      addNumber1 = [[NSNumber alloc] initWithInt:];
      [addDictionary1 setValue:addName1 forKey:@"name"];
      [addDictionary1 setValue:addNumber1 forKey:@"list"];

      //更改key对应的数据
     if ([[WBBooksManager sharedInstance] isBookExistsForKey:@"Add1"]) {
         [[WBBooksManager sharedInstance] replaceDictionary:addDictionary1 withDictionaryKey:@"Add1"];
     }
     resultDictionary = [[WBBooksManager sharedInstance]readPlist];
     NSLog(@"add1111的结果%@", resultDictionary); 

     //删除给出key对应的数据
     if ([[WBBooksManager sharedInstance] isBookExistsForKey:@"Add1"]) {
         [[WBBooksManager sharedInstance] removeBookWithKey:@"Add1"];
     }
     resultDictionary = [[WBBooksManager sharedInstance]readPlist];
     NSLog(@"add1111的结果%@", resultDictionary); 

     //删除整个plist文件
 //    if ([[WBBooksManager sharedInstance]isPlistFileExists]) {
 //    [[WBBooksManager sharedInstance] deletePlist];
 //    }

     resultDictionary = [[WBBooksManager sharedInstance]readPlist];
     NSLog(@"add1111的结果%@", resultDictionary);
  

iOS开发——数据持久化OC篇&plist文件增删改查操作的更多相关文章

  1. iOS开发——数据持久化Swift篇&通用文件存储

    通用文件存储 import UIKit class ViewController: UIViewController { @IBOutlet weak var textField: UITextFie ...

  2. IOS开发中使用CNContact\CNMutableContact 对通讯录增删改查

    IOS开发中使用CNContact\CNMutableContact 对通讯录增删改查 首先当然是把CNcontact包含在工程中: @import Contacts; 1.下面是增加联系人的程序段: ...

  3. iOS开发-plist文件增删改查

    plist第一次看到这个后缀名文件的时候感觉怪怪的,不过接触久了也就习以为常了,plist是Property List的简称可以理解成属性列表文件,主要用来存储串行化后的对象的文件.扩展名为.plis ...

  4. iOS开发——数据持久化Swift篇&使用Core Data进行数据持久化存储

    使用Core Data进行数据持久化存储   一,Core Data介绍 1,Core Data是iOS5之后才出现的一个数据持久化存储框架,它提供了对象-关系映射(ORM)的功能,即能够将对象转化成 ...

  5. iOS开发——数据持久化Swift篇&SettingBundle

    SettingBundle import UIKit class ViewController: UIViewController { var userDefault = NSUserDefaults ...

  6. iOS开发数据持久化技术02——plist介绍

    有疑问的请加qq交流群:390438081 我的QQ:604886384(注明来意) 微信:niuting823 1. 简单介绍:属性列表是一种xml格式的文件.扩展名.plist: 2. 特性:pl ...

  7. iOS开发——数据持久化Swift篇&(二)沙盒文件

    沙盒文件 //******************** 5.2 文件操作 func use_FileOperations() { //1.获取程序的Home目录 let homeDirectory = ...

  8. iOS开发——数据持久化Swift篇&文件目录路径获取(Home目录,文档目录,缓存目录等)

    文件目录路径获取(Home目录,文档目录,缓存目录等)   iOS应用程序只能在自己的目录下进行文件的操作,不可以访问其他的存储空间,此区域被称为沙盒.下面介绍常用的程序文件夹目录:   1,Home ...

  9. iOS开发——数据持久化Swift篇&iCloud云存储

    iCloud云存储 import UIKit class ViewController: UIViewController { override func viewDidLoad() { super. ...

随机推荐

  1. anible包模块管理

    ansible使用包管理模块 一般使用的包管理模块的RPM,和YUM 参数 必填 默认 选择 说明 Conf_file No YUM的配置文件 Disable_dbg_check No No Yes/ ...

  2. 数往知来 asp.net 聊天室问题解决方案<十六>

      1:在服务端创建了一个负责监听的sokcet   //三个:采用TCP协议.              ListenSocket = new Socket(AddressFamily.InterN ...

  3. python install 2.7.10

    CentOS 6.5升级Python和安装IPython 后来换成了CentOS 6.5,系统自带的Python版本是2.6.6. 图一:安装IPython需求 已经安装好gcc等编译工具.系统自带P ...

  4. 性能测试-ApacheBench

    基本简介 ApacheBench 是一个指令列程式,专门用来执行网站服务器的运行效能,特别是针对Apache 网站服务器.这原本是用来检测 Apache 网站服务器能够提供的效能,特别是可以看出Apa ...

  5. 【MySql】在Linux下安装MySql数据库

    [参数环境] 1.Host OS:Win7 64bit 2.VM: VMware 11.1.0 3.Client OS:CentOS 6 4.系统中已安装的openssl版本: openssl-1.0 ...

  6. 关于诺顿身份安全2013独立版(Norton Identity Safe)

    现在身份安全这货好像从诺顿的套装当中独立出来了,出了中文版.其实诺顿的Web信誉做得还是不错的,当然天朝不要有太大期望.只是公认的做web信誉做得最好的应该就是趋势科技和诺顿,所以诺顿的身份安全也许还 ...

  7. VPN 隧道协议PPTP、L2TP、IPSec和SSLVPN的区别

    最近软矿频繁地介绍了各种VPN,有免费的PacketiX.NET和Hotspot Shield,有付费的Astrill VPN,iVPN和PureVPN.在介绍这些VPN的时候,常常会说到PPTP.L ...

  8. oracle学习 十二 使用.net程序调用带返回值的存储过程(持续更新)

    数据库返回的是结果集,存储过程返回的是一个或者多个值,所以不要使用while循环去读取,也不要使用datareader函数去调用.v_class_name是返回函数 使用.net调用oracle数据库 ...

  9. 结构类模式(六):享元(Flyweight)

    定义 运用共享技术有效的支持大量细粒度的对象. 两个状态 内蕴状态存储在享元内部,不会随环境的改变而有所不同,是可以共享的. 外蕴状态是不可以共享的,它随环境的改变而改变的,因此外蕴状态是由客户端来保 ...

  10. 那些年困扰我们的委托(C#)

    委托这个东西不是很好理解,可是工作中又经常用到,你随处可以看到它的身影,真让人有一种又爱又恨的感觉,我相信许多人被它所困扰过. 一提到委托,如果你学过C语言,你一定会马上联想到函数指针. 什么是委托? ...