其实写入不到真机里面主要是你写入时,当前那文件夹里你要写入的plist根本不存在

所以你怎么写都写不进去,所以你必须先创建你要写入的plist;

你用这样初始化程序就能自己创建:

  1. NSMutableArray *dictplist = [[NSMutableArray alloc] init];
  2. [dictplist insertObject:markName atIndex:0];
  3. [dictplist writeToFile:plistPath atomically:YES];

直接下面这种是不行的

  1. NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
  2. [array insertObject:markName atIndex:0];
  3. [array writeToFile:plistPath atomically:YES];

下面是具体的实现方法

一般plist 的写入位置在

写入文件的位置:(Library文件夹)

  1. NSString *lib = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
  2. NSString *libPath = [lib stringByAppendingString:@"/Caches"];
  3. NSString* plistPath = [libPath stringByAppendingFormat:@"/bookmark.plist"];
  4. NSLog(@"%@",plistPath);
  5. if(![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
  6. NSMutableArray *dictplist = [[NSMutableArray alloc] init];
  7. [dictplist insertObject:markName atIndex:0];
  8. [dictplist writeToFile:plistPath atomically:YES];
  9. NSLog(@"------1-----%@",dictplist);
  10. }
  11. else
  12. {
  13. NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
  14. [array insertObject:markName atIndex:0];
  15. [array writeToFile:plistPath atomically:YES];
  16. NSLog(@"-------2----%@",array);
  17. }

写入文件的位置:( Document 文件夹)

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  2. //获取完整路径
  3. NSString *documentsDirectory = [paths objectAtIndex:0];
  4. NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"test2.plist"];
  5. NSLog(@"%@",plistPath);
  6. if(![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
  7. NSMutableArray *dictplist = [[NSMutableArray alloc] init];
  8. [dictplist insertObject:markName atIndex:0];
  9. [dictplist writeToFile:plistPath atomically:YES];
  10. NSLog(@"------1-----%@",dictplist);
  11. }
  12. else
  13. {
  14. NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
  15. [array insertObject:markName atIndex:0];
  16. [array writeToFile:plistPath atomically:YES];
  17. NSLog(@"-------2----%@",array);
  18. }

下面的是参考 http://blog.csdn.net/smallsky_keke/article/details/7431277

这篇文章是自己通过实践获取,在网上查过很多资料,也走了不上的弯路,由于刚开始学子不久,只是把自己遇到的问题贡献给大家

一,创建文件

//获取路径对象

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES);

//获取完整路径

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"test.plist"];

NSMutableDictionary *dictplist = [[NSMutableDictionaryalloc
] init];

//设置属性值

[dictplist
setObject:@"张三" forKey:@"name"];

[dictplist
setObject:@"李四"forKey:@"name1"];

[dictplist
setObject:@"王五"forKey:@"name2"];

//写入文件

[dictplist
writeToFile:plistPath atomically:YES];

这个是创建了一个简单的plist文件,创建后的图1为:

下面是创建了一种多键值的plist文件,代码和图如下:

//获取路径对象

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES);

//获取完整路径

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"test.plist"];

NSMutableDictionary *dictplist = [[NSMutableDictionaryalloc
] init];

//定义第一个插件的属性

NSMutableDictionary *plugin1 = [[NSMutableDictionaryalloc]init];

[plugin1
setObject:@"张三"forKey:@"name1"];

[plugin1
setObject:@"李四"forKey:@"name2"];

//定义第二个插件的属性

NSMutableDictionary *plugin2 = [[NSMutableDictionaryalloc]init];

[plugin2
setObject:@"王五"forKey:@"name1"];

[plugin2
setObject:@"赵斌"forKey:@"name2"];

//设置属性值

[dictplist
setObject:plugin1 forKey:@"初一班"];

[dictplist
setObject:plugin2 forKey:@"初二班"];

//写入文件

[dictplist
writeToFile:plistPath atomically:YES];

图2:

针对图1进行修改的程序,代码如下:

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES)       objectAtIndex:0]stringByAppendingPathComponent:@"test.plist"];

NSMutableDictionary *applist = [[[NSMutableDictionaryalloc]initWithContentsOfFile:path]mutableCopy];

NSString *name = [applist objectForKey:@"name"];

name =
@"山山";

[applist
setObject:name forKey:@"name"];

[applist
writeToFile:path atomically:YES];

执行后如下图:

针对图2进行修改的程序,代码如下:

[dictplist writeToFile:plistPath atomically:YES];

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES)objectAtIndex:0]stringByAppendingPathComponent:@"test.plist"];

//根据路径获取test.plist的全部内容

NSMutableDictionary *infolist= [[[NSMutableDictionaryalloc]initWithContentsOfFile:path]mutableCopy];

//获取初一班的信息

NSMutableDictionary *info = [infolist objectForKey:@"初一班"];

NSString *name1 = [info objectForKey:@"name1"];

name1 =
@"山山";

[info
setValue:name1 forKey:@"name1"];

[infolist
setValue:info forKey:@"初一班"];

[infolist
writeToFile:path atomically:YES];

图如下:

以上两个修改信息的地方,必须要加入红色标记的方法,才能在表里进行增改操作。

以上是个人的学习心得,请大家多多指教。

数据持久化—真机上的Plist写入的更多相关文章

  1. iOS数据持久化-OC

    沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...

  2. 数据持久化-Plist文件写入

    数据持久化,常见4种:归档,plist文件,sqlite,coreData.今天复习的是plist文件读写. // // ViewController.m // Test_Plist // // Cr ...

  3. iOS - OC 数据持久化

    1.Sandbox 沙箱 iOS 为每个应用提供了独立的文件空间,一个应用只能直接访问为本应用分配的文件目录,不可以访问其他目录,每个应用自己独立的访问空间被称为该应用的沙盒.也就是说,一个应用与文件 ...

  4. iOS - Swift 数据持久化

    1.Sandbox 沙箱 iOS 为每个应用提供了独立的文件空间,一个应用只能直接访问为本应用分配的文件目录,不可以访问其他目录,每个应用自己独立的访问空间被称为该应用的沙盒.也就是说,一个应用与文件 ...

  5. iOS中 数据持久化 UI高级_17

    数据持久化的本质就是把数据由内写到本地(硬盘中),在iOS指将数据写到沙盒文件夹下: 沙盒机制:指的就是采用沙盒文件夹的形式管理应用程序的本地文件,而且沙盒文件夹的名字是随机分配的,采用十六进制方法命 ...

  6. iOS -数据持久化方式-以真实项目讲解

    前面已经讲解了SQLite,FMDB以及CoreData的基本操作和代码讲解(CoreData也在不断学习中,上篇博客也会不断更新中).本篇我们将讲述在实际开发中,所使用的iOS数据持久化的方式以及怎 ...

  7. iOS之数据持久化方案

    概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) ...

  8. Docker数据持久化与容器迁移

    上节讲到当容器运行期间产生的数据是不会在写镜像里面的,重新用此镜像启动新的容器就会初始化镜像,会加一个全新的读写入层来保存数据.如果想做到数据持久化,Docker提供数据卷(Data volume)或 ...

  9. iOS的数据持久化

    所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) pr ...

随机推荐

  1. MMCM与PLL

    MMCM与PLL   1.the clock management title(CMT) 弄清楚BUFR, IBUFG,BUFG,GT,BUFH,是什么. 2.MMCM内部结构 3.PLL内部结构 4 ...

  2. 值得收藏的JSP连接mysql数据库的例子

    1:用mysql驱动把mysql与tomcat的连接起来.把mysql驱动包(不用解压)放到Tomcat安装目录中lib文件夹下即可. 2:然后在自己的新建的web应用程序上面就可以下下面的代码 3: ...

  3. 忽略时间的小时分,展示的方法 data函数

    date(create_at) 列表: sql:

  4. flume-拦截器、channel选择器、sink组合sink处理器

    1. Flume Interceptors Flume有能力修改/删除流程中的events.这是在拦截器(interceptor)的帮助下完成的.拦截器(Interceptors)是实现org.apa ...

  5. ospf精确宣告地址

    ospf的一点小问题 http://bbs.51cto.com/thread-881459-1.html 参照博客地址 network 172.20.1.0 0.0.0.3 area 0 networ ...

  6. tesseract 中文二次训练

    tesseract4.0以上版本可参考 https://github.com/tesseract-ocr/tesseract/wiki/TrainingTesseract-4.00#tutorial- ...

  7. re模块小结

    一.引子: 文件err.txt中有如下内容: 要求提取出所有的电话号码来. 方法一:文件操作法: f = open('eer.txt','r',encoding='utf-8') l = [] for ...

  8. [UE4]制作视野图标

    一.在PhotoShop中新建一个64px*64px,背景色为透明的文档 二.填充“图层1”为白色 三.添加蒙版图层,并将前景色设置为黑色 四.将“图层1”选择45°,并往上拖放至如图位置 五.选择渐 ...

  9. 更新SAS 9.4(64位) SID的简单方法(可以使用至2018.04.30)

    打开SAS,在程序窗口输入: PROC SETINIT RELEASE='9.4'; SITEINFO NAME='NATIONAL PINGTUNG UNI OF SCIENCE&TECH' ...

  10. 第8章 传输层(2)_UDP协议

    2. 用户数据报协议(UDP) 2.1 UDP的特点 (1)UDP是无连接的,即发送数据之前不需要建立连接,因此减少了开销和发送数据之前的时延. (2)UDP使用了尽最大努力交付,即不保证可靠交付,因 ...