美文网首页
plist文件操作

plist文件操作

作者: Show撑腰 | 来源:发表于2016-01-10 16:55 被阅读0次

    #define YDYWriteToPlist(data, filename) [data writeToFile:[NSString stringWithFormat:@"/Users/XXXXXX/Desktop/%@.plist", filename] atomically:YES];

    - (IBAction)save:(id)sender {

    // 1.plist存储,生成一个plist文件.

    // 2.plist不是数组就是字典,plist存储就是用来存储字典或者数组.

    //注意:Plist不能存储自定义对象

    Person*p = [[Personalloc]init];

    //创建数组

    NSArray*arr =@[@"123",@1,@"abc"];

    //存储数据,存储应用沙盒(应用的文件夹中)

    //沙盒路径/Caches文件/文件名

    //获取沙盒路径

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

    //直接应用沙盒中Caches文件路径

    // directory:获取哪个文件夹

    // domainMask:在哪个范围内搜索,NSUserDomainMask:表示在用户的手机上查找

    // expandTilde:是否展开全路径YES:表示展开全路径NO:不会展开全路径,会把应用沙盒的路径用波浪号(~)代替

    //获取到Caches文件夹路径

    NSString*cachePath =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)[0];

    //拼接文件名

    NSString*filePath = [cachePathstringByAppendingPathComponent:@"arr.plist"];

    // File:文件的全路径

    [arrwriteToFile:filePathatomically:YES];

    }

    //点击读取的时候调用

    - (IBAction)read:(id)sender {

    //获取到Caches文件夹路径

    NSString*cachePath =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)[0];

    //拼接文件名

    NSString*filePath = [cachePathstringByAppendingPathComponent:@"arr.plist"];

    //读取plist,之前是什么类型存储的,读取也是什么

    NSArray*arr = [NSArrayarrayWithContentsOfFile:filePath];

    NSLog(@"%@",arr);

    }

    相关文章

      网友评论

          本文标题:plist文件操作

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