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

  • 相关阅读:
    linq 查询-“必须是可缩小的节点”
    在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。”
    将字符串转换为double类型的list
    mysql新增用户无法授权!? 解决方案
    eclipse项目有红色感叹号
    错误: 找不到或无法加载主类 java操作hbase出错
    org.apache.hadoop.conf.Configuration无法引用 解决方法
    jdbc的数据库驱动类DriverManager.getConnection()详解
    MySQL [Err] 1055--1064
    控制反转
  • 原文地址:https://www.cnblogs.com/allanliu/p/4229754.html
Copyright © 2020-2023  润新知