Search code examples
iosswiftnsfilemanager

Using NSFileManager and createDirectoryAtPath in Swift


I'm trying to create a new folder, but I can't figure out how to use createDirectoryAtPath correctly.

According to the documentation, this is the correct syntax:

NSFileManager.createDirectoryAtPath(_:withIntermediateDirectories:attributes:error:)

I tried this:

let destinationFolder: String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let deliverablePath: NSURL = NSURL.fileURLWithPath("\(destinationFolder)/\(arrayOfProjectIDs[index])")!
NSFileManager.createDirectoryAtPath(deliverablePath, withIntermediateDirectories: false, attributes: nil, error: nil)

But this gives me the error

Extra argument 'withIntermediateDirectories' in call

I've also tried a lot of variations, removing parameters and so on, but I can't get it to run without an error. Any ideas?


Solution

  • You forgot to add defaultManager() and to convert the NSURL to String.

    You can try replacing

    NSFileManager.createDirectoryAtPath(deliverablePath, withIntermediateDirectories: false, attributes: nil, error: nil)

    with this (converting your NSURL to String)

    var deliverablePathString = deliverablePath.absoluteString
    
    NSFileManager.defaultManager().createDirectoryAtPath(deliverablePathString, withIntermediateDirectories: false, attributes: nil, error: nil)
    

    Hope this helps