其实写入不到真机里面主要是你写入时,当前那文件夹里你要写入的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. BugFree使用技巧 转载

    转载 2013年05月27日 17:53:45 BugFree使用技巧 我们将会从使用Ldap域账号.使用标记.发送邮件通知.更改上传附件的大小和使用快捷键来进行讲解. 使用Ldap域帐号 如果希望使 ...

  2. WebService C#开发/调用

    简单描述C#开发WebService操作步骤以及调用方式 WebService开发 第一步:创建Web空项目 第二步:为创建的Web空项目添加Web服务 第三步:实现WebService方法(仅供参考 ...

  3. Java第07次实验提纲(异常)

    PTA与参考资料 题集:集合 异常实验文件 第1次实验 1.1 7-1 常用异常 如何进行强制转换.父类型转化为子类型常见错误. 如何捕获多种类型的异常 简要输出异常信息,System.out.pri ...

  4. java中的自定义注解的使用

    https://www.cnblogs.com/acm-bingzi/p/javaAnnotation.html

  5. VS项目平台的x86,x64,Any CPU以及Debug和Release

    引用链接:https://blog.csdn.net/zuguangboy/article/details/51509670

  6. Hadoop概念学习系列之Hadoop、Spark学习路线(很值得推荐)(十八)

    不多说,直接上干货! 说在前面的话 此笔,对于仅对于Hadoop和Spark初中学者.高手请忽略! 1 Java基础: 视频方面:          推荐<毕向东JAVA基础视频教程>.学 ...

  7. mongodb并列查询,模糊查询

    在mongodb的查询语句中可以这么写{“a”:$gt(1),"a":$lt(5)} 但这么查询出来的值会做单个条件匹配,最终结果为a大于1的集合+a小于5的集合 如果需要实现去交 ...

  8. 传统Java Web(非Spring Boot)、非Java语言项目接入Spring Cloud方案

    技术架构在向spring Cloud转型时,一定会有一些年代较久远的项目,代码已变成天书,这时就希望能在不大规模重构的前提下将这些传统应用接入到Spring Cloud架构体系中作为一个服务以供其它项 ...

  9. shell命令输出

    在shell脚本中的打印输出通常会有echo和printf两种,前者会自动换行. 一.echo Shell 的 echo 指令与 PHP 的 echo 指令类似,都是用于字符串的输出.您可以使用ech ...

  10. sqlserver 数据简单查询

    use StudentManageDB go select StudentName as 姓名,Gender as 性别,出生日期=birthday from Students where Gende ...