Swift plist文件读写

<pre name="code" class="plain">
  1. //写入plist
  2. var
    paths =
    NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,
    NSSearchPathDomainMask.UserDomainMask,
    true)
    //Get Path of Documents Directory
  3. var
    documentsDirectory:AnyObject
    =
    paths[0]
  4. var
    path =
    documentsDirectory.stringByAppendingPathComponent("samplePlist.plist")
  5. var
    fileManager =
    NSFileManager.defaultManager()
  6. var
    fileExists:Bool
    =
    fileManager.fileExistsAtPath(path)
  7. var
    data :
    NSMutableDictionary?
  8. //Check
    if plist file exists at path specified
  9. if
    fileExists ==
    false
    {
  10. //File
    does not exists
  11. data
    =
    NSMutableDictionary
    ()
    //Create data dictionary for storing in plist
  12. }
    else
    {
  13. //File
    exists – retrieve data from plist inside data dictionary
  14. data
    =
    NSMutableDictionary(contentsOfFile:
    path)
  15. }
  16. data?.setValue("\(countButton)",
    forKey:
    "NumeroButton")
  17. data?.writeToFile(path,
    atomically:
    true)
    //Write data to file permanently

//读plist


<ol class="linenums" style="margin: 0px 0px 0px 3em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; font-family: monospace; vertical-align: baseline; list-style: none; color: rgb(62, 62, 62); line-height: 22px; white-space: pre; background-color: rgb(250, 250, 250);"><li class="L0" style="margin: 5px 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; list-style: decimal !important;"><span class="com" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(136, 0, 0);">//Get path of Documents directory</span></li><li class="L1" style="margin: 5px 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; background-color: rgb(240, 240, 240); list-style: decimal !important;"><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);">    </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 221);">var</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> paths </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 0, 102);">NSSearchPathForDirectoriesInDomains</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">(</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 0, 102);">NSSearchPathDirectory</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">.</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 0, 102);">DocumentDirectory</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 0, 102);">NSSearchPathDomainMask</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">.</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 0, 102);">UserDomainMask</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 221);">true</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">)</span></li><li class="L2" style="margin: 5px 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; list-style: decimal !important;"><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);">    </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 221);">var</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> documentsDirectory</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">:</span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 0, 102);">AnyObject</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> paths</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">[</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 102, 102);">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">]</span></li><li class="L3" style="margin: 5px 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; background-color: rgb(240, 240, 240); list-style: decimal !important;"><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);">     </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 221);">var</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> path </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> documentsDirectory</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);">stringByAppendingPathComponent</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">(</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 136, 0);">"samplePlist.plist"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">)</span></li><li class="L4" style="margin: 5px 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; list-style: decimal !important;"><span class="com" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(136, 0, 0);">//Retrieve contents from file at specified path</span></li><li class="L5" style="margin: 5px 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; background-color: rgb(240, 240, 240); list-style: decimal !important;"><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);">    </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 221);">var</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> data </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 0, 102);">NSMutableDictionary</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);">contentsOfFile</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);"> path</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">!)</span></li><li class="L6" style="margin: 5px 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; list-style: decimal !important;"><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);">   println</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(0, 0, 0);">path</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(102, 102, 0);">)</span></li></ol>

原文地址:http://iphonedevsdk.com/forum/iphone-sdk-development/120452-swift-save-reload-data-to-plist.html
时间: 2024-10-13 12:38:33

Swift plist文件读写的相关文章

ios开发入门- plist 文件读写

在xcode中创建一个项目 会在supportion files 目录下 看见一个已plist后缀类型的文件,这个文件 是以 key value 存放的键 值对的值.它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plist ,因此通常被称为 plist文件.plist文件是标准的xml格式的.  我们在日常开发中 可以用它 来存储 一些系统的 的用户信息,系统的配置信息等. 本章 我们来看看plist 文件是 如何读写的 在具体

Plist文件读写操作

在xcode中创建一个项目 会在supportion files 目录下 看见一个已plist后缀类型的文件,这个文件 是以 key value 存放的键 值对的值.它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plist ,因此通常被称为 plist文件.plist文 件是标准的xml格式的.  我们在日常开发中 可以用它 来存储 一些系统的 的用户信息,系统的配置信息等. 本章 我们来看看plist 文件是 如何读写的 在具

plist 文件读写

//1. 创建一个plist文件    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);    NSString *path=[paths    objectAtIndex:0];    NSLog(@"path = %@",path);    NSString *filename=[path stringByAppendingPathCompone

文件操作2之plist文件操作

DionysosLai([email protected]) 2015/1/19 Plist文件属于一种比较常见文件格式,通常用来存储用户设置,也可以用于存储捆绑信息,原先是用在Mac中,后来推广到其他系统中.概况来说,Plist文件是以.plist格式结尾,xml格式的一种文件,读写方式比较基本以'键'= '值'方式对应. 本文系统的介绍基于cocos2dx-2.3版本的plist文件读写细节,希望通过本文,对其他plist文件能够达到自由读取目的. plist 资源文件如下所示: <?xml

数据持久化-Plist文件写入

数据持久化,常见4种:归档,plist文件,sqlite,coreData.今天复习的是plist文件读写. // // ViewController.m // Test_Plist // // Created by lidongbo on 14/10/30. // Copyright (c) 2014年 lidongbo. All rights reserved. // #import "ViewController.h" @interface ViewController () @

iOS学习之 plist文件的读写

在做iOS开发时,经经常使用到到plist文件,  那plist文件是什么呢? 它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plist ,因此通常被称为 plist文件.文件是xml格式的. Plist文件通经常使用于储存用户设置,也能够用于存储捆绑的信息 我们创建一个项目来学习plist文件的读写. 1.创建项目Plistdemo 项目创建之后能够找到项目相应的plist文件,打开例如以下图所看到的: 在编辑器中显示相似与表

数据持久化之文件读写,属性列表(plist)

待完善 数据持久化之文件读写,属性列表(plist),布布扣,bubuko.com

Swift - 本地数据的保存与加载(使用NSCoder将对象保存到.plist文件)

下面通过一个例子将联系人数据保存到沙盒的“documents”目录中.(联系人是一个数组集合,内部为自定义对象). 功能如下: 1,点击“保存”将联系人存入userList.plist文件中 2,点击“读取”从数据文件中加载解析出联系人 注意: 1,本例使用了NSCoder,这个封装了许多技术细节,使用它我们可以很轻易的将对象写到文件中,也可以用它将文件中的对象转换回来. 2,自定义对象必须添加如下两个方法,这个才能顺利的被序列化编码存储和读取. 1 2 3 4 5 6 7 8 9 10 11

IOS读写Plist文件最简单方法

在网上搜了一圈,发现好多代码 = =小白不想看那么多啊,然后就琢磨了下,原来这么easy ! 1 // 获取PLIST路径 2 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"]; 3 // 获取PLIST数据 4 NSMutableDictionary *data = [[NSMutableDictionary alloc] initWith