数据储存之plist文件

plist是Property List的缩写,是用于存储串行化对象的XML格式文件,常见扩展名为.plist。它主要支持存储NSArray、NSDictionary、NSString、NSData、NSDate、NSNumber等类型数据,通常将这些数据封装在字典中进行保存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

plist是:全名Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。文件是xml格式的,只能存储NSArray、NSDictionary、NSString、NSData、NSDate、NSNumber类型数据,而且一般我们我会把NSArray、NSDictionary、NSString、NSData、NSDate、NSNumber都存储在一个字典里。


plist文件用于储存少量数据,读取速度快,如果数据量太多,耗费内存太大,读取速度会很慢,应该储存在数据库中。


1.plist文件的创建

   //获取Document路径
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    //将创建的plist文件的名称拼接到路径后面,plist文件的名称
    NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];
    //创建plist文件
    
    //判断当前是不是存在test.plist文件
    if([[NSFileManager defaultManager] fileExistsAtPath:filename])
    {
        //存在直接进行操作
    
    }else{
    
        //创建plist文件
        NSFileManager* fm = [NSFileManager defaultManager];
        [fm createFileAtPath:filename contents:nil attributes:nil];
    }
   
2.对plist进行操作

    //为plist文件写入数据
    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:@"love",@"name",@"20",@"age",nil];
    [dic writeToFile:filename atomically:YES];

    
    //读取plist文件中的数据
    NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithContentsOfFile:filename];
    NSLog(@"dic is:%@",dic2);
    
    //修改plist文件中的数据
    [dic2  setObject:@"you" forKey:@"name"];
    [dic2 writeToFile:filename atomically:YES];
    
    //删除plist文件
    [fileManager removeItemAtPath:filename error:nil];

以上为plist文件的使用,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值