title: Swift: write JSON and save to iPhone File locally
body: <p>I am learning how to write JSON format data to an iPhone. So with one button clicked, I want to save those data to the iPhone.  I looked a simple look on how to write some simple text and saved it to the iPhone file and it worked. However, I tried to apply the same idea to JSON data but still haven't figure out. I tried:</p>
  
  <p>Rather than having contents equal to some written text, I tried to put it as my data (jsondata).
  But it does not seem working with </p>
  
  <p>try jsondata.write(to: fileURL, atomically: false, encoding: .utf8)</p>
  
  <pre><code>@IBAction func writeFiles(_ sender: Any) {
          let file = "\(UUID().uuidString).txt"
                 let contents = "Testing"
                 let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
                 let fileURL = dir.appendingPathComponent(file)
                 do {
                     try contents.write(to: fileURL, atomically: false, encoding: .utf8)
                 }
                 catch {
                     print("Error: \(error)")
                 }
     }
  </code></pre>
  
  <p>Sorry I am a bit new and learning Swift at the moment</p>
  
  <p>Thanks!</p>