数据持久化—真机上的Plist写入
其实写入不到真机里面主要是你写入时,当前那文件夹里你要写入的plist根本不存在
所以你怎么写都写不进去,所以你必须先创建你要写入的plist;
你用这样初始化程序就能自己创建:
- NSMutableArray *dictplist = [[NSMutableArray alloc] init];
- [dictplist insertObject:markName atIndex:0];
- [dictplist writeToFile:plistPath atomically:YES];
直接下面这种是不行的
- NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
- [array insertObject:markName atIndex:0];
- [array writeToFile:plistPath atomically:YES];
下面是具体的实现方法
一般plist 的写入位置在
写入文件的位置:(Library文件夹)
- NSString *lib = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
- NSString *libPath = [lib stringByAppendingString:@"/Caches"];
- NSString* plistPath = [libPath stringByAppendingFormat:@"/bookmark.plist"];
- NSLog(@"%@",plistPath);
- if(![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
- NSMutableArray *dictplist = [[NSMutableArray alloc] init];
- [dictplist insertObject:markName atIndex:0];
- [dictplist writeToFile:plistPath atomically:YES];
- NSLog(@"------1-----%@",dictplist);
- }
- else
- {
- NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
- [array insertObject:markName atIndex:0];
- [array writeToFile:plistPath atomically:YES];
- NSLog(@"-------2----%@",array);
- }
写入文件的位置:( Document 文件夹)
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- //获取完整路径
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"test2.plist"];
- NSLog(@"%@",plistPath);
- if(![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
- NSMutableArray *dictplist = [[NSMutableArray alloc] init];
- [dictplist insertObject:markName atIndex:0];
- [dictplist writeToFile:plistPath atomically:YES];
- NSLog(@"------1-----%@",dictplist);
- }
- else
- {
- NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
- [array insertObject:markName atIndex:0];
- [array writeToFile:plistPath atomically:YES];
- NSLog(@"-------2----%@",array);
- }
下面的是参考 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写入的更多相关文章
- iOS数据持久化-OC
沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...
- 数据持久化-Plist文件写入
数据持久化,常见4种:归档,plist文件,sqlite,coreData.今天复习的是plist文件读写. // // ViewController.m // Test_Plist // // Cr ...
- iOS - OC 数据持久化
1.Sandbox 沙箱 iOS 为每个应用提供了独立的文件空间,一个应用只能直接访问为本应用分配的文件目录,不可以访问其他目录,每个应用自己独立的访问空间被称为该应用的沙盒.也就是说,一个应用与文件 ...
- iOS - Swift 数据持久化
1.Sandbox 沙箱 iOS 为每个应用提供了独立的文件空间,一个应用只能直接访问为本应用分配的文件目录,不可以访问其他目录,每个应用自己独立的访问空间被称为该应用的沙盒.也就是说,一个应用与文件 ...
- iOS中 数据持久化 UI高级_17
数据持久化的本质就是把数据由内写到本地(硬盘中),在iOS指将数据写到沙盒文件夹下: 沙盒机制:指的就是采用沙盒文件夹的形式管理应用程序的本地文件,而且沙盒文件夹的名字是随机分配的,采用十六进制方法命 ...
- iOS -数据持久化方式-以真实项目讲解
前面已经讲解了SQLite,FMDB以及CoreData的基本操作和代码讲解(CoreData也在不断学习中,上篇博客也会不断更新中).本篇我们将讲述在实际开发中,所使用的iOS数据持久化的方式以及怎 ...
- iOS之数据持久化方案
概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) ...
- Docker数据持久化与容器迁移
上节讲到当容器运行期间产生的数据是不会在写镜像里面的,重新用此镜像启动新的容器就会初始化镜像,会加一个全新的读写入层来保存数据.如果想做到数据持久化,Docker提供数据卷(Data volume)或 ...
- iOS的数据持久化
所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) pr ...
随机推荐
- gcc与g++的一些关系
Gcc 简介Linux系统下的gcc(GNU C Compiler)是GNU推出的功能强大.性能优越的多平台编译器,是GNU的代表作品之一.Gcc是可以在多种硬体平台上编译出可执行程序的超级编译器,其 ...
- json server的简单使用(附:使用nodejs快速搭建本地服务器)
作为前端开发人员,经常需要模拟后台数据,我们称之为mock.通常的方式为自己搭建一个服务器,返回我们想要的数据.json server 作为工具,因为它足够简单,写少量数据,即可使用. 安装 首先需要 ...
- 宝塔linux面板运行jsp文件的配置工作
第一步宝塔安装和软件安装我们先安装宝塔面板(这个不需要我说咋弄吧) 安装完成后登录到宝塔面板然后安装软件我个人喜欢nginx最新版,mysql由于服务器配置很菜所以没发装56,php什么的我用不到就没 ...
- jsp页面间对象传递方法
严格的来说不能叫做JSP页面间的对象传递,实际应该是页面间对象共享的方法: 1. 通过servletcontext,也就是application对象了,但这种情况要求在同一个web应用下, ...
- Lucene 4.3 - Facet demo
package com.fox.facet; import java.io.IOException; import java.util.ArrayList; import java.util.List ...
- Jenkins XVnc Plugin
Linux下的Jenkins里配置Webdriver项目会碰到如下错误 org.openqa.selenium.firefox.NotConnectedException: Unable to con ...
- 【C#】语音识别 - System.Speech
一个有趣的东西,今后可能用得上. C#语音识别:在命名空间 System.Speech下SpeechSynthesizer可以将文字转换成语音 贴出代码: public partial class F ...
- Android开发教程之【环境搭建及HelloWorld】(转载)
引言 本系列适合0基础的人员,因为我就是从0开始的,此系列记录我步入Android开发的一些经验分享,望与君共勉!作为Android队伍中的一个新人的我,如果有什么不对的地方,还望不吝赐教. 在开始A ...
- 配置文件elasticsearch.yml详解
在es根目录下的config目录中有elasticsearch.yml配置文件,es加载使用的yml格式配置 17行:cluster.name: 自定义集群名称(强烈推荐默认名称elasticsear ...
- spring boot (入门简介 demo)
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...