<pre name="code" class="plain">
- //写入plist
- var
paths =
NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,
NSSearchPathDomainMask.UserDomainMask,
true)
//Get Path of Documents Directory - var
documentsDirectory:AnyObject
=
paths[0] - var
path =
documentsDirectory.stringByAppendingPathComponent("samplePlist.plist") - var
fileManager =
NSFileManager.defaultManager() - var
fileExists:Bool
=
fileManager.fileExistsAtPath(path) - var
data :
NSMutableDictionary? - //Check
if plist file exists at path specified - if
fileExists ==
false
{ - //File
does not exists - data
=
NSMutableDictionary
()
//Create data dictionary for storing in plist - }
else
{ - //File
exists – retrieve data from plist inside data dictionary - data
=
NSMutableDictionary(contentsOfFile:
path) - }
- data?.setValue("\(countButton)",
forKey:
"NumeroButton") - 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