Plist文件写入数组(包含读取,插入和删除)

作者: Jack__yang | 来源:发表于2016-08-30 21:45 被阅读3459次

在iOS开发过程中,我们会经常用到数据持久化问题,作为数据持久化解决方案之一,plist的使用是一个很方便快捷的方案。

plist是什么?
它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。文件是xml格式的。Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息。

有关Plist文件写入数组(包含写入,读取和删除)

  • plist保存的地方
    1,工程沙盒里(就是程序user Document文件夹下,以读取文件,写入文件方式)
    2,工程自身里(就是在工程里手动创建一个如.plist文件,把固定的内容写入,这个需要人工手动写入)
    3,工程沙盒里(保存到user Document下,不过不需要读写文件,用系统的 NSUserDefaults 可以快速保存添加读取删除基本数据类型,类似于android里的Sharedpreferences )

以下方法可直接调用:

且注意以下都是以plist文件的root为数组的;

请根据自己的需求使用;

以下只有数据的简单处理,并未解析,请按照所你需要的数据结构进行解析。

1.Plist文件写入

-(void)writeFileToplist
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    //获取完整路径
    
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Test.plist"];//这里就是你将要存储的沙盒路径(.plist文件,名字自定义)
    
    NSLog(@"%@",plistPath);
 
    if(![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {//plistPath这个文件\文件夹是否存在
   // - 什么是NSFileManager

 //  顾名思义, NSFileManager是用来管理文件系统的
//它可以用来进行常见的文件\文件夹操作
//NSFileManager使用了单例模式

//使用defaultManager方法可以获得那个单例对象
//[NSFileManager defaultManager]

        NSMutableArray *dictplist = [[NSMutableArray alloc] init];
        [dictplist insertObject:_myInfo_array atIndex:0];
        
        [dictplist writeToFile:plistPath atomically:YES];
        NSLog(@"------1----- %@写入不成功",dictplist);
    }
    else
    {
        NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
        [array insertObject:_myInfo_array atIndex:0];
        
        [array writeToFile:plistPath atomically:YES];
        NSLog(@"-------2----%@写入成功",array);
    }
}

2.plist文件读取

#pragma  mark 读取xcode中的plist文件
-(void)loadXcodeFileToplist
{
//读取Property List文件(Xcode中的)
 NSString *userDataPath = [[NSBundle mainBundle] pathForResource:@"New" ofType:@"plist"]; 
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:userDataPath]; 
NSLog(@"%@",data);
}
#pragma  mark 读取沙盒中的plist文件
-(void)loadFileToplist
{
 //获取路径
        NSArray *sandboxpath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *filePath = [[sandboxpath objectAtIndex:0] stringByAppendingPathComponent:@"Test.plist"];
        NSLog(@"%@",NSHomeDirectory());
        //获取数据
                NSArray *arrMain = [NSArray arrayWithContentsOfFile:filePath];

}

3.plsit文件删除

#pragma  mark 读取并删除沙盒中的plist文件

-(void)deleteFileToplist
{
    NSFileManager *manager=[NSFileManager defaultManager];
    //文件路径
    NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"Test.plist"];//这里就是你将要删除的沙盒路径(.plist文件,名字自定义)

    if ([manager removeItemAtPath:filepath error:nil]) {
        NSLog(@"文件删除成功");
    }
}

相关文章

  • Plist文件写入数组(包含读取,插入和删除)

    在iOS开发过程中,我们会经常用到数据持久化问题,作为数据持久化解决方案之一,plist的使用是一个很方便快捷的方...

  • Plist文件写入数组(包含读取,插入和删除)

    在iOS开发过程中,我们会经常用到数据持久化问题,作为数据持久化解决方案之一,plist的使用是一个很方便快捷的方...

  • IOS plist 文件写入与读取

    数组写入plist文件(文件储存到cache路径下) 字典写入plist文件 将字典数组写入plist文件

  • Python中txt的简单操作

    Python操作txt文件的基础操作记录,包含新建文件、读取文件、写入文件、替换文件、修改文件后缀和插入数据。1....

  • plist文件的创建和读取

    课程要点:plist文件的新建与读取给UITableView设置变化的值单元格的删除、插入及刷新 plist...

  • txt读写

    文件打开 读文件 读取字符串 按行读取整个文件 写文件 字符串写入txt 列表写入文件 双层列表写入文件 数组写入文件

  • swift读取和写入plist文件

    var diaryList:String = NSBundle.mainBundle().pathForResou...

  • plist文件的读取 写入

    手动创建的plist文件只能手动写入,代码写入的放在沙盒中 NSString*pathP = [[NSBundle...

  • plist 文件写入与读取

    plist是iOS中特有的一种文件形式,将数据写入plist文件的实质就是生成plist文件,那什么样的数据才能生...

  • iOS中plist文件

    plist文件 plist文件储存本地数据的一种方式 json实例对象,字典或者数组 跟目录 写入数据到plist...

网友评论

本文标题:Plist文件写入数组(包含读取,插入和删除)

本文链接:https://www.haomeiwen.com/subject/pmhqettx.html