• IOS错误笔记(一)--------plist


    要写入plist文件,数据类型是有限制的: NSInteger, CGFloat, NSString, NSDate 不能存储自定义对象数据

    在一个project中存在两种plist     

    第一种是通过 commond + N 以手动创建plist文件方式创建的; 第二种是直接用代码通过路径生成的;

    第一种  plist存放在app中,只可读,不可写。第二种存放在工程的documents文件夹中,可读写,用于存放需修改的数据;

    第一种plist文件手动添加操作,不需要代码。以下为将plist文件应用于程序中的步骤

    1.取路径              NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];

    2.通过路径名将数据赋给数组     NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path

    ------------------------------------------------------------------------------------------------------------------ 

    以下为第二种plist文件的创建,修改,删除 等方法

    1.根据project所在目录创建路径      NSArray *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

                        NSString *documentsDirectory = [ doc objectAtIndex:0 ];

    2.找到路径后在目录下新建plist文件    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"test.plist"];

    3.将新建plist文件的数据赋给dictplist   NSMutableDictionary *dictplist = [[NSMutableDictionary alloc ] init];

    4.用dictplist给plist文件赋值              [dictplist writeToFile:plistPath atomically:YES];

    5.改plist文件,一般都是将其赋给一个  对象(数组,字典)再对数组或字典进行增减,然后将其再赋给plist文件。

     -----------------------------------------------------------------------------------------------------------

    个人猜想:

      基于“存在的都是合理的这一概念”   ,两种plist既然如此设计,一定有其道理。

      我觉得是第一种plist是用来初始化数据,比如第一次运行的一些默认数据。第二种是用来存储用户个人数据的,可不断的增减。

    ---------------------------------------------------------------------------------------------------------------------------------------------------------

    以下为个人猜想的实践,第一次通过app中的plistdemo.plist初始化本地的test.plist,之后直接从本地读取test.plist来初始化数据。

    - (void)viewDidLoad {
    
        [super viewDidLoad];
        //读取本地documents中的路径
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *plistPath1 = [paths objectAtIndex:0];
        
        //得到完整的文件名
        NSString *filename=[plistPath1 stringByAppendingPathComponent:@"test.plist"];
        NSDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
        
        //如果test文件不存在,则用手动创建的plistdemo.plist文件来初始化
        if (dict == nil) {
             NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"];
             NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
            [data writeToFile:filename atomically:YES];
        //第一次初始化的时候回输出ok!
            NSLog(@"ok!");
        }
        //最后检测test文件数据是否修改成功
        NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
        NSLog(@"%@", data1);
    
    }
    

      

    http://blog.csdn.net/smallsky_keke/article/details/7431277

  • 相关阅读:
    [C]static变量详解
    [LINUX]重定向
    [PHP]一些坑
    [PHP]常量的一些特性
    [数学]三角函数(一)
    [PHP]session的一些要点
    [C]控制外部变量访问权限的extern和static关键字
    c语言基础----共用体
    c语言基础----字符串数组
    c语言基础----函数库
  • 原文地址:https://www.cnblogs.com/fsliu/p/4242662.html
Copyright © 2020-2023  润新知