zoukankan      html  css  js  c++  java
  • plist文件操作总结

     

    plist文件类 (负责文件的读写,删除整个文件)

    #import "Plist.h"

    @implementation Plist


    - (void)writePlist:(NSMutableDictionary*)dictionary

    {

        NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES);

        NSString *docPath = [[array objectAtIndex:0] stringByAppendingPathComponent:PLISTNAME];

        [dictionary writeToFile:docPath atomically:YES];

        [array release];

    }


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

    {

        NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES);

        NSString *docPath = [[array objectAtIndex:0]stringByAppendingPathComponent:PLISTNAME];

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

    }


    - (void)deletePlist

    {

        NSFileManager *fileManager = [NSFileManager defaultManager];

        NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES);

        NSString *docPath = [[array objectAtIndex:0] stringByAppendingPathComponent:PLISTNAME];

        [fileManager removeItemAtPath:docPath error:nil];

    }

    @end

    - (void)viewDidLoad

    {

        [super viewDidLoad];


        //文件读写


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

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

        NSMutableArray *array = [[NSMutableArray alloc] init];

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

        [dictionary1 setValue:@"001" forKey:@"harewareID"];

        [array addObject:dictionary1];

        [dictionary setValue:array forKey:@"CPU"];   

         self.plist = [[Plist alloc] init];

         //调用写文件

         [self.plist writePlist:dictionary];

         //读文件

        [self.plist readPlist:&dictionary2];

        NSMutableArray *array1 = [[NSMutableArray alloc] init];

        array1 = [dictionary2 objectForKey:@"CPU"];

        NSString *str = [[array1 objectAtIndex:0] objectForKey:@"harewareID"];

        NSLog(@"%@",str);



       //文件删除

      

       [self.plist deletePlist];

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

        [self.plist readPlist:&dictionary3];

        NSMutableArray *array2 = [[NSMutableArray alloc] init];

        array2 = [dictionary3 objectForKey:@"CPU"];

        NSString *str2 = [[array2 objectAtIndex:0] objectForKey:@"harewareID"];

        NSLog(@"%@",str2);


      //文件内容更改,更改一条数据就是把dictionary内key重写。这里重新插入harewareID


      

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

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

        NSMutableArray *array5 = [[NSMutableArray alloc] init];

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

        [dictionary7 setValue:@"002" forKey:@"harewareID"];

        [array5 addObject:dictionary7];

        [dictionary6 setValue:array5 forKey:@"CPU"];

        [self.plist writePlist:dictionary6];

        [self.plist readPlist:&dictionary5];

        NSMutableArray *array4 = [[NSMutableArray alloc] init];

        array4 = [dictionary5 objectForKey:@"CPU"];

        NSString *str4 = [[array4 objectAtIndex:0] objectForKey:@"harewareID"];

        NSLog(@"%@",str4);

  • 相关阅读:
    主动一点
    白鱼泡湿地公园
    Google管理制度(东北分公司)
    碎嘴子
    升级的时代
    SD2.0大型网站架构讨论沙龙
    旅途摘抄《12条职场秘笈》
    fscache 调研
    lxccheckpoint 一些 比较老的资料
    folly,facebook刚刚开源的底层c++函数
  • 原文地址:https://www.cnblogs.com/allanliu/p/4229754.html
Copyright © 2011-2022 走看看