OC数据储存本地(二)-----iOS----属性列表plist 写如何存储和读取

本文介绍如何利用plist文件存储和读取数组数据。详细解释了如何将数组写入plist文件并从文件中恢复数组内容的方法。同时提供了示例代码,展示了不同路径设置方式及其在iOS开发中的应用。

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

属性列别 plist文件:这个文件可以保存数组,可以吧数组中的元素保存这个文件中


 将数组的信息,存储到plist文件中,就会将数组的所有元素存储到这个文件中

 - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

 plist文件中的数据还原成一个数组

 + (nullable NSArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path;


 

//    获取doc路径

//    1.拼接字符串

//    NSString *homePath = NSHomeDirectory();

//    NSString *docPath = [homePath stringByAppendingString:@"/Documents"];

//    NSLog(@"%@",docPath);

    

//  下面这个 不用/ 来拼接方法  - (NSString *)stringByAppendingPathComponent:(NSString *)str;


//    NSString *homePath = NSHomeDirectory();

//    NSString *docPath = [homePath stringByAppendingPathComponent:@"Documents"];

//    NSLog(@"%@----",docPath);

    

    

    //    2。用第二中比较科学,因为第一种必须是要用Documents

//    通过搜索   通过搜索  FOUNDATION_EXPORT NSArray<NSString *> *NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde);


    

    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

    

    NSString *filePath = [docPath stringByAppendingPathComponent:@"Property List.plist"];

//    数组

    NSArray *array = @[@"123",@"good",@"zzhuangx",@"装逼如风"];

    

    [array writeToFile:filePath atomically:YES];

    

    NSLog(@"%@",docPath);

    




下面这中的也可以



#import <Foundation/Foundation.h>


int main(int argc,constchar * argv[]) {

   

    NSArray *arr =@[@"ddd",@"srose",@"lili",@"luck"];

    

    [arr writeToFile:@"/Users/moyan/Desktop/abc.plist"atomically:NO];

//    出来Yes的时候,桌面上会出来abc.plist文件

    NSLog(@"Yes");

    for (NSString *strin arr) {

        NSLog(@"%@",str);

    }

    

//    NSArray *arr = [NSArray arrayWithContentsOfFile:@"/Users/moyan/Desktop/abc.plist"];

//        for (NSString *str in arr) {

//            NSLog(@"%@",str);

//        }

//    

    

    return0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值