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);

  • 相关阅读:
    MarkDown语法
    AxureRP 序列号
    数据库选型相关
    linux 防火墙
    SpringMVC控制器方法参数传入的ModelMap 和Model类型有啥区别
    2017中国软件技术大会参会总结
    SpringMVC的Model ModeMap ModelAndView @ModelAttribute @SessionAttribute区分
    mybatis调用oracle存储过程 out游标类型参数 如何赋给java map
    mybatis 调用 oracle 存储过程 select into 无记录时NO_DATA_FOUND异常处理分析
    mybatis 调用oracle存储过程如何返回out参数值
  • 原文地址:https://www.cnblogs.com/allanliu/p/4229754.html
Copyright © 2011-2022 走看看