title: Write to plist file in Swift
body: <p>I have a sample plist-file, favCities.plist. This is my sample code:</p>
  
  <pre><code> override func viewDidLoad() {
      super.viewDidLoad()
  
  
      let path = NSBundle.mainBundle().pathForResource("favCities", ofType: "plist")
      var plistArray = NSArray(contentsOfFile: path) as [Dictionary&lt;String, String&gt;]
  
      var addDic: Dictionary = ["ZMV": "TEST", "Name": "TEST", "Country": "TEST"]
      plistArray += addDic
      (plistArray as NSArray).writeToFile(path, atomically: true)
  
      var plistArray2 = NSArray(contentsOfFile: path)
  
      for tempDict1 in plistArray2 {
          var tempDict2: NSDictionary = tempDict1 as NSDictionary
          var cityName: String = tempDict2.valueForKey("Name") as String
          var cityZMV: String = tempDict2.valueForKey("ZMV") as String
          var cityCountry: String = tempDict2.valueForKey("Country") as String
          println("City: \(cityName), ZMV: \(cityZMV), Country: \(cityCountry)")
      }
  </code></pre>
  
  <p>At first glance, everything works well. The output looks like this:</p>
  
  <pre><code>City: Moscow, ZMV: 00000.1.27612, Country: RU
  City: New York, ZMV: 10001.5.99999, Country: US
  City: TEST, ZMV: TEST, Country: TEST
  </code></pre>
  
  <p>But when I interrupt the app, I see that my file favCities.plist has not changed. There are still two values. These values ​​- <code>City: TEST, ZMV: TEST, Country: TEST</code> - were not added. If I restarted the application, then again I see 3 lines of output, although there should be 4.</p>
  
  <p>What is wrong?</p>
  
  <p><strong>UPDATED:</strong></p>
  
  <p>I was changed code to this:</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
      let fileManager = (NSFileManager.defaultManager())
      let directorys : [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
  
      if (directorys! != nil){
          let directories:[String] = directorys!;
          let dictionary = directories[0];
  
          let plistfile = "favCities.plist"
          let plistpath = dictionary.stringByAppendingPathComponent(plistfile);
  
          println("\(plistpath)")
  
          var plistArray = NSArray(contentsOfFile: plistpath) as [Dictionary&lt;String, String&gt;]
  
          var addDic: Dictionary = ["ZMV": "TEST", "Name": "TEST", "Country": "TEST"]
          plistArray += addDic
  
          (plistArray as NSArray).writeToFile(plistpath, atomically: false)
  
          var plistArray2 = NSArray(contentsOfFile: plistpath)
  
          for tempDict1 in plistArray2 {
              var tempDict2: NSDictionary = tempDict1 as NSDictionary
              var cityName: String = tempDict2.valueForKey("Name") as String
              var cityZMV: String = tempDict2.valueForKey("ZMV") as String
              var cityCountry: String = tempDict2.valueForKey("Country") as String
              println("City: \(cityName), ZMV: \(cityZMV), Country: \(cityCountry)")
          }
      }
      else {
          println("ERROR!")
      }
  
  }
  </code></pre>
  
  <p>Now when you run the application the number of rows in the output increases:</p>
  
  <pre><code>City: Moscow, ZMV: 00000.1.27612, Country: RU
  City: New York, ZMV: 10001.5.99999, Country: US
  City: TEST, ZMV: TEST, Country: TEST
  City: TEST, ZMV: TEST, Country: TEST
  ........
  </code></pre>
  
  <p>BUT! If view the file favCities.plist, which is located in the project folder (Project Navigator in Xcode), it still remains unchanged - there are two lines!</p>
  
  <p>If walk along the path, which is stored in the variable <code>plistpath</code> - <code>/Users/admin/Library/Developer/CoreSimulator/Devices/55FD9B7F-78F6-47E2-9874-AF30A21CD4A6/data/Containers/Data/Application/DEE6C3C8-6A44-4255-9A87-2CEF6082A63A/Documents/</code></p>
  
  <p>Then there is one more file favCities.plist. It contains all the changes that make the application. What am I doing wrong? How can I see all the changes in a file that is located in the project folder (Project Navigator)?</p>