iOS plist文件增删改查

一. plist简介
  • plist文件,即属性列表文件,全名是Property List,这种文件的扩展名为.plist,因此,通常被叫做plist文件。它是一种用来存储串行化后的对象的文件,在iOS开发中通常用来存储用户设置,还可以用于存储程序中经常用到而不经常改动的数据。下面就看一下如何创建和读写plist文件。
  • plist 只能存储基本的数据类型 和 array 字典

二. 首先是使用xcode自带的功能使用plist
1. 根据图片的顺序创建一个新的plist文件
2. 给plist命名
  • 需要注意的问题
    • 命名的时候不能用Info.plist , INfo.plist, xxxInfo.plist等形式,否则会出现下面的情况,因为系统中存在一个Info.plist文件,会发生冲突
3. 然后就可以在xcode中编辑plist文件了
4. 使用plist文件
  • 这里一般只能在读取plist文件,具体获取plist文件路径的代码为:

    复制代码
      NSString *filePath = [[NSBundle mainBundle] pathForResource:@"newsModel" ofType:@"plist"];
  • 然后是获取plist里面的数据,这里跟下面是相同的


三. 使用代码的方式使用plist
1. 写入plist代码演示
复制代码
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [path objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"newsTest.plist"];
    //创建数据
    NSMutableDictionary *newsDict = [NSMutableDictionary dictionary];
    //赋值
    [newsDict setObject:@"zhangsan" forKey:@"name"];
    [newsDict setObject:@"12" forKey:@"age"];
    [newsDict setObject:@"man" forKey:@"sex"];
    //数据写入plist
    [newsDict writeToFile:plistPath atomically:YES];
2. 读取plist文件代码演示
复制代码
    //获取plist文件的路径
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path1 = [pathArray objectAtIndex:0];
    NSString *myPath = [path1 stringByAppendingPathComponent:@"newsTest.plist"];

    //根据之前保存的容器类型读取数据
    //是数组就用数组来获取数据,是字典就用字典来获取数据
    //newsModel.plist文件
    //NSMutableArray *data1 = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    //newsTest.plist文件
    NSMutableDictionary *data2 = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
3. plist的增删操作
  • 增删操作就是对读取后的plist数据容器进行的增删,然后保存就好
相关推荐
2501_9160088910 小时前
全面介绍Fiddler、Wireshark、HttpWatch、SmartSniff和firebug抓包工具功能与使用
android·ios·小程序·https·uni-app·iphone·webview
Haha_bj14 小时前
Flutter ——flutter_screenutil 屏幕适配
android·ios
Haha_bj14 小时前
Flutter ——device_info_plus详解
android·flutter·ios
山水域18 小时前
SKAdNetwork 6.0 深度实战:多窗口转化值(Conversion Value)建模与数据分层架构
ios
JavinLu20 小时前
ios 配置了代理且使用 chls.pro/ssl 下载不了证书,无法弹出下载证书的提示问题
网络协议·ios·ssl
G311354227321 小时前
免费苹果 Plist 文件在线制作 iOS IPA 安装工具
ios
2401_832298101 天前
免费p12证书在线检测iOS苹果证书状态一键查询
ios
符哥20081 天前
Swift 开发 iOS App 过程中写自定义控件的归纳总结
ios·cocoa·swift
pop_xiaoli1 天前
effective-Objective-C 第二章阅读笔记
笔记·学习·ios·objective-c·cocoa
未来侦察班1 天前
一晃13年过去了,苹果的Airdrop依然很坚挺。
macos·ios·苹果vision pro