码迷,mamicode.com
首页 > 编程语言 > 详细

Swift plist文件读写

时间:2015-01-05 18:49:22      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:swift   ios   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

Swift plist文件读写

标签:swift   ios   plist   读写   

原文地址:http://blog.csdn.net/tpian928/article/details/42425125

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!