{"id":94,"date":"2017-02-03T20:26:57","date_gmt":"2017-02-03T20:26:57","guid":{"rendered":"http:\/\/luduscella.com\/blog\/\/?p=94"},"modified":"2017-02-03T20:38:50","modified_gmt":"2017-02-03T20:38:50","slug":"swift-creating-plist-programmatically","status":"publish","type":"post","link":"http:\/\/luduscella.com\/blog\/swift\/swift-creating-plist-programmatically","title":{"rendered":"Swift: Creating Plist Programmatically"},"content":{"rendered":"<p>It\u2019s practical to use <a href=\"https:\/\/en.wikipedia.org\/wiki\/Property_list\" target=\"_blank\">plist<\/a> in files for iOS projects either to store configuration settings or to store any other information you may need in the execution of your application.<\/p>\n<p>The plist or property list structure is key-value relation:<\/p>\n<p>example:<\/p>\n<p><a href=\"http:\/\/luduscella.com\/blog\/\/wp-content\/uploads\/2017\/02\/plist.png\"><img loading=\"lazy\" src=\"http:\/\/luduscella.com\/blog\/\/wp-content\/uploads\/2017\/02\/plist-300x99.png\" alt=\"plist\" width=\"300\" height=\"99\" class=\"alignnone size-medium wp-image-96\" srcset=\"http:\/\/luduscella.com\/blog\/wp-content\/uploads\/2017\/02\/plist-300x99.png 300w, http:\/\/luduscella.com\/blog\/wp-content\/uploads\/2017\/02\/plist-1024x338.png 1024w, http:\/\/luduscella.com\/blog\/wp-content\/uploads\/2017\/02\/plist.png 1254w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Before we try to generate the plist file we can check if the plist exists:<\/p>\n<pre class=\"lang:swift decode:true \" title=\"swift\" >\r\n\r\nlet fileManager = FileManager.default\r\nlet documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String\r\n    let path = documentDirectory.appending(\"\/example.plist\")\r\n\r\n<\/pre>\n<p>In case the file doesn\u2019t exist:<\/p>\n<pre class=\"lang:default decode:true \" title=\"plist\" >    if (!fileManager.fileExists(atPath: path)) {\r\n            \/*\r\n                        here is where we are going to create the plist file\r\n            *\/\r\n    }\r\n\r\n<\/pre>\n<p> To generate the plist file lets start with the content. In this case we are going to use the capitals of the states:<\/p>\n<pre class=\"lang:swift decode:true \" >let dicContent:[String: String] = [\"Alabama\": \"Montgomery\", \"Alaska\":\"Juneau\",\"Arizona\":\"Phoenix\"]<\/pre>\n<p>Now we are going to convert the dictionary content to <a href=\"https:\/\/developer.apple.com\/reference\/foundation\/nsdictionary\" target=\"_blank\">NSDictionary<\/a>:<\/p>\n<pre class=\"lang:swift decode:true \" >let plistContent = NSDictionary(dictionary: dicContent)<\/pre>\n<p>With the content already in NSDictionary we can try to write to disk:<\/p>\n<pre class=\"lang:swift decode:true \" >let success:Bool = plistContent.write(toFile: path, atomically: true)<\/pre>\n<p>We can check if write to disk was successful<\/p>\n<pre class=\"lang:swift decode:true \" > \r\nif success {\r\n\tprint(\"file has been created!\")\r\n}else{\r\n\tprint(\"unable to create the file\")\r\n}\r\n<\/pre>\n<pre class=\"lang:swift decode:true \" >\r\n\t func pListCreation() {\r\n           let fileManager = FileManager.default\r\n            let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String\r\n            let path = documentDirectory.appending(\"\/example.plist\")\r\n           \r\n            if (!fileManager.fileExists(atPath: path)) {\r\n                let dicContent:[String: String] = [\"Alabama\": \"Montgomery\", \"Alaska\":\"Juneau\",\"Arizona\":\"Phoenix\"]\r\n                let plistContent = NSDictionary(dictionary: dicContent)\r\n                let success:Bool = plistContent.write(toFile: path, atomically: true)\r\n                if success {\r\n                    print(\"file has been created!\")\r\n                }else{\r\n                    print(\"unable to create the file\")\r\n                }\r\n               \r\n            }else{\r\n                print(\"file already exist\")\r\n            }\r\n        }\r\n<\/pre>\n<p>Please let me know if you have any questions by leaving a comment below or on twitter <a href=\"https:\/\/twitter.com\/luisemedr\" target=\"_blank\">@luisemedr<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It\u2019s practical to use plist in files for iOS projects either to store configuration settings or to store any other information you may need in the execution of your application. The plist or property list structure is key-value relation: example: Before we try to generate the plist file we can check if the plist exists: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[17,16,15,10],"_links":{"self":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts\/94"}],"collection":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":12,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":107,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions\/107"}],"wp:attachment":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}