Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 888
Naftali Beder Avatar asked Mar 30 '15 01:03

Naftali Beder


2 Answers

The Swift 2.0 way:

do {
    var deliverablePathString = "/tmp/asdf"
    try NSFileManager.defaultManager().createDirectoryAtPath(deliverablePathString, withIntermediateDirectories: false, attributes: nil)
} catch let error as NSError {
    NSLog("\(error.localizedDescription)")
}
like image 114
seb Avatar answered Nov 14 '22 03:11

seb


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

like image 35
CrApHeR Avatar answered Nov 14 '22 02:11

CrApHeR