1

I'm using the following code (lifted from another answer) to create a folder in the documents directory (my app requires an export of a file). I'm trying to name this folder but the folder is creating with the name of the Xcode project rather than the name I have specified :

func createFileDirectory() {
    let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])

//set the name of the new folder
    let folderPath = documentsPath.appendingPathComponent("MY NAME HERE")
    do
    {
        try FileManager.default.createDirectory(atPath: folderPath!.path, withIntermediateDirectories: true, attributes: nil)
    }
    catch let error as NSError
    {
        NSLog("Unable to create directory \(error.debugDescription)")
    }
}

So when the user is able to save to files, it should allow them to save to a folder called "MY FOLDER HERE" but it just creates a folder with the name of the Xcode project ("MY_PROJECT")

I can't find any other answers other than the one that gave me the answer in the first place here

I can't see where I'm doing anything wrong? It's creating a folder it's just ignoring the name?

UPDATE

Following answers I've updated to the URL API method and removed spaces from the target folder name but still getting the wrong folder name (of the project name)

updated code:

func createFileDirectory() {
    let documentsURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)

    //set the name of the new folder
    let folderURL = documentsURL.appendingPathComponent("FirstDraft")
    do
    {
        try FileManager.default.createDirectory(at: folderURL, withIntermediateDirectories: true)
    }
    catch let error as NSError
    {
        NSLog("Unable to create directory \(error.debugDescription)")
    }
}
4
  • 1
    Use URL instead of NSURL. Commented Jun 27, 2019 at 16:37
  • 2
    I’d suggest you edit your code in your question to print(folderURL) and also show us what the folderURL is. This code, from vadian, is correct. When I run this on a simulator, I see /Users/user/Library/Developer/CoreSimulator/Devices/76F6650B-...-A43327F2A267/data/Containers/Data/Application/3F1DCBEE-...-EA7F58957EEC/Documents/FirstDraft, which is correct. Commented Jun 27, 2019 at 19:33
  • Thanks I’ll do this when I can get back to the computer - are you able to go to Files and see if the name is reflected in the folder displayed there also? Commented Jun 27, 2019 at 20:25
  • this works now! Commented Jun 27, 2019 at 21:26

1 Answer 1

4

Please use the URL related API. It's much more reliable

func createFileDirectory() {
    let documentsURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)

    //set the name of the new folder
    let folderURL = documentsURL.appendingPathComponent("MY NAME HERE")
    do
    {
        try FileManager.default.createDirectory(at: folderURL, withIntermediateDirectories: true)
    }
    catch let error as NSError
    {
        NSLog("Unable to create directory \(error.debugDescription)")
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

hey thanks - this isn't working for me in simulator but i'll try on a real device soon – nc14 just now
unfortunately this isn't working on a real device either - it's just showing the folder name as the name of the project still
"Application supports iTunes file sharing" set to YES

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.