iOS中plist的创建,数据写入与读取功能创建一个test.plist文件,textInput作为输入,displayLabel作为显示,有一个按钮来触发保持程序triggerStorage;

-(void)triggerStorage
{
    displayLabel.text = textInput.text;
    
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths    objectAtIndex:0];  
    NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];   //获取路径
    
    NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];  //读取数据
    NSLog(@"dic2 is:%@",dic2); 
    
    //创建一个dic,写到plist文件里
    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:displayLabel.text,@"IP",nil]; //写入数据
    [dic writeToFile:filename atomically:YES];   
 
}
 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    NSMutableArray *resultData; 
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSLog(@"path = %@",path);   
    NSString *filename=[path stringByAppendingPathComponent:@"test.plist"]; 
  
    //读文件
    NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];
    NSLog(@"dic is:%@",dic2);    
    if(dic2 == nil)
    {
        //1. 创建一个plist文件 
        NSFileManager* fm = [NSFileManager defaultManager];
        [fm createFileAtPath:filename contents:nil attributes:nil];        
    }
    else
    {
        resultData=[dic2 objectForKey:@"IP"]; 
        if([dic2 count] > 0)
        {
            displayLabel.text = resultData;
        }
        else
        {
            displayLabel.text = @" ";
        }
    }
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]   //按钮的初始化及触发条件设置
                                               initWithTitle:@"保存" 
                                               style:UIBarButtonItemStylePlain 
                                               target:self 
                                               action:@selector(triggerStorage)] autorelease];    
    [super viewDidLoad];
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
参考代码:
plist 文件读写
    //1. 创建一个plist文件
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);  www.2cto.com
    NSString *path=[paths    objectAtIndex:0];
    NSLog(@"path = %@",path);
    NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];    
    NSFileManager* fm = [NSFileManager defaultManager];
    [fm createFileAtPath:filename contents:nil attributes:nil];        
    //NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    
    //创建一个dic,写到plist文件里
    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:@"sina",@"1",@"163",@"2",nil];
    [dic writeToFile:filename atomically:YES];
    
    //读文件
    NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];
    NSLog(@"dic is:%@",dic2);
 
相关文章:
http://blog.csdn.net/totogo2010/article/details/7634185
http://www.2cto.com/kf/201302/188061.html
 
 

iOS plist文件创建的更多相关文章

  1. iOS plist文件的读写

    原帖:http://blog.csdn.net/totogo2010/article/details/7634185 在做iOS开发时,经常用到到plist文件,  那plist文件是什么呢? 它全名 ...

  2. IOS , plist 配置项说明

    本文转载至 http://blog.csdn.net/fengsh998/article/details/8307424 Key:Application can be killed immediate ...

  3. iOS - Plist 数据解析

    前言 NS_AVAILABLE(10_6, 4_0) @interface NSPropertyListSerialization : NSObject 如果对象是 NSArray 或 NSDicti ...

  4. IOS plist轻量级操作

    plist,全名PropertyList,即属性列表文件,它是一种用来存储串行化后的对象的文件.这种文件,在ios开发过程中经常被用到.这种属性列表文件的扩展名为.plist,因此通常被叫做plist ...

  5. iOS Plist 文件的 增 删 改

    一:Plist 文件的创建 Plist 文件作为我们IOS开发的一种数据存储文件,还是经常会用到的.在看<X-code江湖录>的时候,看到了这一点.自己就写了一下,把写的东西分享出来!先看 ...

  6. IOS plist的数据 存 取(沙河目录)

    应用沙盒目录的常见获取方式 沙盒根目录:NSString *home = NSHomeDirectory(); Documents:(2种方式) 1.利用沙盒根目录拼接”Documents”字符串 N ...

  7. iOS——plist的创建,数据写入与读取

    iOS中plist的创建,数据写入与读取 Documents:应用将数据存储在Documents中,但基于NSuserDefaults的首选项设置除外Library:基于NSUserDefaults的 ...

  8. iOS,plist文件、pct文件,工程设置

    1.使用pch文件 2.在info.plist中配置URL Schemes 3.plist配置拍照界面,复制,粘贴等菜单的显示语言 显示中文 4.使用非ARC库/ARC库 5.链接选项-Objc &a ...

  9. iOS Plist文件,增删改查

    今天早上,9点开始弄Plist,然后一直写,一直写(中午取出40分钟吃饭时间),写到1点,写完了,交给头,头说,不是这个意思.我是每个用户创建了一个文件夹,在这个用户的文件夹里面,分别根据应用创建了文 ...

随机推荐

  1. 虚拟机VMware与主机共享文件介绍

    我们经常会在Windows平台安装虚拟机VMware,不管是出于实验测试还是工作需要,伴随而来的就是经常需要在Windows系统和虚拟机系统之间进行共享数据文件,例如,需要将Window主机上的Ora ...

  2. String与int之间的转换

    原文: http://www.360doc.com/content/10/1215/00/2258566_78225883.shtml

  3. Euclid求最大公约数

    Euclid求最大公约数算法 #include <stdio.h> int gcd(int x,int y){ while(x!=y){ if(x>y) x=x-y; else y= ...

  4. 理解 Cinder 架构 - 每天5分钟玩转 OpenStack(45)

    从本节开始我们学习 OpenStack 的 Block Storage Service,Cinder 理解 Block Storage 操作系统获得存储空间的方式一般有两种: 通过某种协议(SAS,S ...

  5. Git的使用的简单介绍

    节选自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 git是一个分布式的引擎, ...

  6. 使用好压(HaoZip)软件打包EverEdit制作安装程序

    最近使用EverEdit,使用原始的安装程序安装后,需要重新安装插件,对配置文件进行了修改,定制了工具栏.将安装后的程序目录进行打包,制作新的安装包,便于携带. 以下为打包制作过程: 打包原料:Eve ...

  7. 理解ThreadLocal(之一)

    ThreadLocal是什么 在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁地编 ...

  8. OpenSessionInView模式

    首先搭建建构 引入jar包 创建实体类  Emp.java public class Emp { private Integer empId;//员工ID private String empname ...

  9. java多线程系类:JUC原子类:04之AtomicReference原子类

    概要 本章对AtomicReference引用类型的原子类进行介绍.内容包括:AtomicReference介绍和函数列表AtomicReference源码分析(基于JDK1.7.0_40)Atomi ...

  10. NOI2018准备Day12

    上午学了1个小时左右的指针,学了个从句子中分离单词的方法,其他的感觉没学到啥. 中午看了一会儿网络流,懵逼...... A了8道题,4道钻石.3道黄金.1道白银,自己写出了codevs"解药 ...