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. Linux命令学习总结:chage

    命令简介: 该命令用于密码时效管理.它可以修改账号和密码的有效期.对于chage命令的描述如下所示: The chage command changes the number of days betw ...

  2. asp.net mvc 之旅—— 第四站 学会用Reflector调试我们的MVC框架代码

    我们知道,现在能调试.net程序通常有两个,第一个是ILSpy,还是一个是Reflector,这两个小反编译软件算是我们研究底层代码中所拥有的一把 锋利小尖刀~~~,比如你看到的ILSpy这样的界面图 ...

  3. CANopen学习——OSI模型复习

    CanOpen协议实现了物理层和数据链路层,OSI模型是完整的7层. OSI模型网络七层包括物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 物理层:提供信息传输的物理连接通道,包括使用的 ...

  4. Visual Studio 2015 和 Apache Cordova 跨平台开发入门(一)

    基于 Windows 10 的 Visual Studio 2015 跨平台的应用开发主要分为基于Visual Studio 安装 Xamarin 扩展的跨Android.iOS 和 Windows的 ...

  5. Jsp 错题分析

    ArrayList删除元素通过RemoveAt(int index)来删除指定索引值的元素 运行时异常都是RuntimeException类及其子类异常,如NullPointerException.I ...

  6. Struts2 JSON

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式 (本质是一种数据传输格式) 定义json对象 var json={"firstName" ...

  7. Centos7的firewalld配置

    红帽官方的使用文档: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Gu ...

  8. nodeJS基础08:读取图片

    1.读取图片 //server.js var http = require("http"); var readImage = require("./readImage&q ...

  9. centos7.0 安装字体库

    最近在centos7.0下用itextpdf将word文档转成pdf时出现字体丢失的情况.网上找了很多资料,各式各样的原因和解决方法.后来经过一番测试发现是centos7.0 minimal没有安装相 ...

  10. React基础知识

    学习文档(按优先级排列)http://reactjs.cn/react/docs/tutorial-zh-CN.htmlhttp://www.cnblogs.com/Mrs-cc/p/4969755. ...