数据持久化—真机上的Plist写入

时间:2024-01-12 08:22:26

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

下面是创建了一种多键值的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:

数据持久化—真机上的Plist写入

针对图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];

执行后如下图:

数据持久化—真机上的Plist写入

针对图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写入

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

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