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

 

转载于:https://www.cnblogs.com/fsliu/p/4242662.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值