title: IOS: Swift: Append String to a file
body: <p>i have file inside document directory and i want to add some text (it'l be a string in JSON format) to this file. As of now i'm using below line of code to write to the file but it is overriding the file every time i write to it. So is there a better way to handle this situation ? i'm new to swift and can't seem to find a solution. </p>
  
  <pre><code>stringVariable.writeToFile(self.directoryPath, atomically: false, encoding: NSUTF8StringEncoding, error: nil)
  </code></pre>
  
  <p>As it is going to be a file with JSON String's in it, how to check if the text already has some data and assign or remove "," (Comma) from the end of the Json String? </p>
  
  <p>Initially the file contains some text like </p>
  
  <pre><code>{ "documentComments":[]}
  </code></pre>
  
  <p>My JSON String is like as follows,</p>
  
  <pre><code>{ "comment": "MySecondComment", "commentDate": "2015-04-23 17:58:59 +0000", "isSigned": 1, "accountId": 1, "documentId": 3 }
  </code></pre>
  
  <p>By inserting couple of json string i want the file to finally look like below,</p>
  
  <pre><code>{ "documentComments":[
  { "comment": "MyFirstComment", "commentDate": "2015-04-22 17:58:59 +0000", "isSigned": 0, "accountId": 1, "documentId": 2 },
  { "comment": "MySecondComment", "commentDate": "2015-04-23 17:58:59 +0000", "isSigned": 1, "accountId": 1, "documentId": 3 }]}
  </code></pre>
  
  <p>Thanks in Advance</p>