7

I'm new to swift and I'm not sure how to make a new folder from a string path (or from some kind of File/ NSFile object)

This is on OS X with Cocoa.

3 Answers 3

18

My understanding is that you are trying to create a directory programmatically using swift. The code given below does the same.

    var err: NSErrorPointer = nil
    let manager = NSFileManager.defaultManager()
    manager.createDirectoryAtPath("/Users/abc/Desktop/swiftDir", withIntermediateDirectories: true, attributes: nil, error: err)
Sign up to request clarification or add additional context in comments.

4 Comments

What if the user isn't called "abc"? What if the user is running on a network and has a completely different location for his file system. Rather than making assumptions about the user's file system, use the tools to ask for the location of special directories.
This is a perfectly valid snippet which answers OP's question. Would it better if the sample path was "/foo/bar/baz"?
You can use NSHomeDirectory() to get the home directory anyway.
You can also use '~', no?
6

Xcode 8 • Swift 3

extension FileManager.SearchPathDirectory {
    func createSubFolder(named: String, withIntermediateDirectories: Bool = false) -> Bool {
        guard let url = FileManager.default.urls(for: self, in: .userDomainMask).first else { return false }
        do {
            try FileManager.default.createDirectory(at: url.appendingPathComponent(named), withIntermediateDirectories: withIntermediateDirectories, attributes: nil)
            return true
        } catch {
            print(error)
            return false
        }
    }
}

Usage:

if FileManager.SearchPathDirectory.desktopDirectory.createSubFolder(named: "untitled folder") {
    print("folder successfully created")
}

SearchPathDirectory

1 Comment

I'd use first instead of directly accessing the first object with a subscript [0]. One returns an optional, the other causes a runtime error if the array is empty.
4

In Swift 2.0 you must use the new style for error handling:

let path: String = "/Users/abc/Desktop/swiftDir"
let fileManager = NSFileManager.defaultManager()
do
{
    try fileManager.createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
    print("Error while creating a folder.")
}

1 Comment

Hi JavaNut. I know that the terminator in this case is not neccessary, but the print syntax is something else, what changed in swift 2.0: My intension was to show, that print("Error while creating a folder.", terminator: "\n") is the new syntax of the print command and if you do not give a terminator, a new line is printed after the text. So someone can learn of showing it in the full syntax. The print command above has differenz meanings in Swift 1 and Swift 2. I wanted to avoid confusion about this.

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.