How to create text file for writing
<p>I've a problem to write text in a file. What I've done so far is the following: I have a string with the text to store and the file name also as a string.</p>

<pre><code>let someText = "abcd"
let fileName = "file:///xxx"
</code></pre>

<p>Of course "xxx" is a .txt file under the document directory so it should be possible to write.<br>
Then I found out that I can use the write method o the string. But for this call I need the file name as url so I have this piece of code:</p>

<pre><code>let fileUrl = URL(string: fileName)
do {
    try someText.write(to: fileUrl, atomically: false, encoding: String.Encoding.utf8)
}
catch { }
</code></pre>

<p>If I start my app then I will get an error "The file xxx does not exist". Ok that's correct because the file is not created. I thought that the write method does it automatically but it seems not so.<br>
And that's the point I don't know how to solve this issue!</p>

<p>I'm using Xcode 8 + Swift 3.</p>

<p>+++ EDIT +++</p>

<p>I try to explain what I'm exactly looking for.<br>
Let's say I've two tasks: The first task builds file names and stores it in a database. That's why I work with strings:</p>

<pre><code>var fileName = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).path
if (!fileName.hasSuffix("/")) {
    fileName += "/"
}
fileName += "file.txt"
</code></pre>

<p>As you can see the file is not created at this moment because I only need the name in this task.</p>

<p>Ok and then the second task. It has to select a specific file name from the database and append it with the file:// scheme:</p>

<pre><code>let fileName = "file://" + fileNameFromDatabase
</code></pre>

<p>Then the text is written with the code above.<br>
It's clear that the file not exists in this task because I only have the name. That's why I think that the error message of the write method is correct.<br>
What I'm now looking for is a possibility to create the file/write the text in one step.</p>