关于plist文件

一、代码创建plist文件:

  NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    //获取完整路径

    NSString *documentsPath = [path objectAtIndex:0];

    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"selectDrugAry.plist"];

以上:如果有这个plist文件,就直接去读取,如果没有这个文件,就直接创建。

 

二、创建一个数组,也可以是一个词典(关键是看你plist文件里面装的是什么)来存plist里面的数据

    NSMutableArray *applist2 = [NSMutableArray arrayWithContentsOfFile:plistPath];

    applist2 = [[NSMutableArray alloc]init];

    for (int s = 0; s<_selectDrugList.count; s++) {

        NSDictionary *dict1= @{

                               @"formats":[_selectDrugList[s]aryForKey:@"formats"],

                               @"img":[_selectDrugList[s] strForKey:@"img"],

                               @"instructions":[_selectDrugList[s] strForKey:@"instructions"],

                               @"medicineId":[_selectDrugList[s] strForKey:@"medicineId"],

                               @"medicineName":[_selectDrugList[s] strForKey:@"medicineName"],

                               @"number":[_selectDrugList[s] strForKey:@"number"],

                               @"producer":[_selectDrugList[s] strForKey:@"producer"],

                               @"selected":[_selectDrugList[s] strForKey:@"selected"],

                               @"shapeName":[_selectDrugList[s] strForKey:@"shapeName"]

                               };

        

        [applist2 addObject:dict1];

    }

    [applist2 writeToFile:plistPath atomically:YES];

    NSMutableArray *ap2 = [NSMutableArray arrayWithContentsOfFile:plistPath];

    NSLog(@"%@",ap2);

 

问题:为什么我从接口得到的,不能直接写进去呢,非要重新自己手写的,创建内容才可以写进去。有知道的,可以评论解惑。

 

三、清空plist,我是把我的数组初始化了,然后写到plist里面的

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    //获取完整路径

    NSString *documentsPath = [path objectAtIndex:0];

    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"usersListTest.plist"];

    NSMutableArray *applist2 = [NSMutableArray arrayWithContentsOfFile:plistPath];

    applist2 = [[NSMutableArray alloc]init];

    [applist2 writeToFile:plistPath atomically:YES];

四、删除plist文件,请百度,很多的。

posted @ 2016-01-08 16:26  SBDB  阅读(138)  评论(0编辑  收藏  举报