NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Trying to create directory in documents folder fails

Forums > iOS

In my iOS app (running in the simulator) I'm able to create some files inside the documents directory with data.write(to: fileUrl) with fileUrl pointing to some file:///Users/Me/Library/Developer/CoreSimulator/..../Documents/myfile.xml.

Now I wanted to create a directory and store the files there (because I want to separate two different kinds of files I want to store), but a call to

try FileManager.default.createDirectory(atPath: pathUrl.absoluteString, withIntermediateDirectories: true)

with pathUrl being similar to the above file:///Users/Me/...../Documents/searches reveals

You can’t save the file “searches” because the volume is read only.

Couldn't find any additional information on what I would need to allow creating directories. Any idea?

Thanks Karsten



how are you creating the pathUrl? Because normally this should work without any extra steps.

This errors looks to me like you are trying to save it somewhere else than the Documens folder


extension FileManager {
    /// Determine document directory.
    /// - Returns:a URL for the document directory.
    static func documentDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return paths[0]

let pathUrl = FileManager.documentDirectory().appendingPathComponent(path)
try FileManager.default.createDirectory(atPath: pathUrl.absoluteString, withIntermediateDirectories: false)

with path being a String, e.g. "searches".

Interesting fact is that the command throws an error with description:

The file “searches” doesn’t exist.

Yes, correct, that's why you should create it!


@nemecek-filip, thanks for looking into it. It made me playing around with it again and triggering some new searches which lead me to: Instead of

try FileManager.default.createDirectory(atPath: pathUrl.absoluteString, withIntermediateDirectories: true)


try FileManager.default.createDirectory(atPath: pathUrl.path, withIntermediateDirectories: true)

which actually works. Thanks


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.